2

In my web application, I have both web API and normal MVC in there I have created an extension for httpResponse

 public static void ShowApplicationError(this HttpResponse response, string exceptionMessage,string innerException)
    {
        var result = JsonConvert.SerializeObject(new { error = exceptionMessage ,detail=innerException });
        response.HttpContext.Response.WriteAsync(result);
    }

and used in startup.cs for exception handling.

app.UseExceptionHandler(builder =>
            {
                builder.Run(async context =>
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    var error = context.Features.Get<IExceptionHandlerFeature>();
                    if (error != null)
                    {
                        context.Response.ShowApplicationError(error.Error.Message, error.Error.InnerException.Message);
                    }
                });
            });

like this. its works fine for both. i want to differentiate the error for each request. i dont want to show the json error for mvc end how do i do that.

4
  • this must work for both... what is the problem? please define your problem Commented Dec 19, 2018 at 9:18
  • i want to know a request for mvc, so i can send the error differently. edited the post Commented Dec 19, 2018 at 9:26
  • AFAIK there's no inbuilt way to do this. Whether it's an API request or an MVC request, its the same internally. Try to separate the API to a specific area then you can use the area token to differentiate the requests and return the relevant type of response Commented Dec 19, 2018 at 9:35
  • Have you tried calling app.UseExceptionHandler before you call app.useMvc? Commented Dec 19, 2018 at 9:44

3 Answers 3

2

You could not distinguish the internal server error from MVC or web api from Error.Message directly. For MVC and Web api, they are both inherited from Controller or ControllerBase.

In general, we distinguish them by adding api to the route path for web api. I suggest you design your project by mvc without api route and web api with api route. Then check the path by ExceptionHandlerFeature.Path.

app.UseExceptionHandler(builder =>
{
    builder.Run(async context =>
    {
        context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
        var error = context.Features.Get<IExceptionHandlerFeature>();
        var error1 = context.Features.Get<IExceptionHandlerFeature>() as ExceptionHandlerFeature;
        var error2 = context.Features.Get<IExceptionHandlerPathFeature>();
        var requestPath = error2.Path;
        if (error != null)
        {
            context.Response.ShowApplicationError(error.Error.Message, error.Error.InnerException.Message);
        }
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

So I have to make deferent projects for each
@Selik There is no need to split into two projects, you could define mvc controller and web api controller in the same project, just make sure add [Route("api/[controller]")] for every web api controller.
oh sorry, you were referring 'ExceptionHandlerFeature.Path' Yup its working.thank you.
2

The best solution for this situation is to separate your concerns better. Make your api a separate csproj from your MVC app. It will give you flexibility to deploy later on as well. If this is existing code and not new code, I would lobby for refactoring it into a separate api project.

1 Comment

That's obvious, but if he have that need, means than he want to make it in that way, maybe because is a test and he don't want to waste a lot of money or something for the school
0

ContentType and Accept Header in HttpRequest differentiates output type which is enough in your case.

you can check by using Accept Header.

if (context.Request.Headers["Accept"] == "application/json" || context.Request.Headers["Accept"] == "application/xml")
{
    //Api Request
}
else
{
    //other request.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.