How to handle unsupported HTTP methods on actions in ASP.NET MVC5?
How do I handle requests to actions that exist, but don't support the http method in the request?
For example, I have the following action:
[HttpGet]
[Route("~/")]
[Route("ServiceVisits")]
public ActionResult Index()
{
return View();
}
If a GET
request comes to http://localhost:59949/
or http://localhost:59949/ServiceVisits
then we get the page back, but if a POST
request comes, then I get an exception in my Application_Error
method in Global.asax.cs
that I have to deal with.
Requests that come to non-existent actions will return a 404
page, because I have the following route:
routes.MapRoute(
"PageNotFound",
"{*.*}",
new {controller = "Error", action = "NotFound"}
);
I'm using Attribute Routing and the above convention is my only one for handling 404's.
I want to log only serious system errors in Application_Error
and not No matching action was found on controller
exceptions. Thank you!
asp.net asp.net-mvc-5
add a comment |
How do I handle requests to actions that exist, but don't support the http method in the request?
For example, I have the following action:
[HttpGet]
[Route("~/")]
[Route("ServiceVisits")]
public ActionResult Index()
{
return View();
}
If a GET
request comes to http://localhost:59949/
or http://localhost:59949/ServiceVisits
then we get the page back, but if a POST
request comes, then I get an exception in my Application_Error
method in Global.asax.cs
that I have to deal with.
Requests that come to non-existent actions will return a 404
page, because I have the following route:
routes.MapRoute(
"PageNotFound",
"{*.*}",
new {controller = "Error", action = "NotFound"}
);
I'm using Attribute Routing and the above convention is my only one for handling 404's.
I want to log only serious system errors in Application_Error
and not No matching action was found on controller
exceptions. Thank you!
asp.net asp.net-mvc-5
In theApplication_Error
you will get all sort of errors, regardless of how you are handling them. If your concern is simply not to log some specific errors, you could add a check in theApplication_Error
handler.
– Tasos K.
Jan 4 at 8:14
@TasosK. Is there no cleaner way of handling http method related 404's? I tried to design my application in a way that only critical server errors would reachApplication_Error
and everything else is handled by filters and such. Cheers!
– Shahin Dohan
Jan 4 at 18:49
add a comment |
How do I handle requests to actions that exist, but don't support the http method in the request?
For example, I have the following action:
[HttpGet]
[Route("~/")]
[Route("ServiceVisits")]
public ActionResult Index()
{
return View();
}
If a GET
request comes to http://localhost:59949/
or http://localhost:59949/ServiceVisits
then we get the page back, but if a POST
request comes, then I get an exception in my Application_Error
method in Global.asax.cs
that I have to deal with.
Requests that come to non-existent actions will return a 404
page, because I have the following route:
routes.MapRoute(
"PageNotFound",
"{*.*}",
new {controller = "Error", action = "NotFound"}
);
I'm using Attribute Routing and the above convention is my only one for handling 404's.
I want to log only serious system errors in Application_Error
and not No matching action was found on controller
exceptions. Thank you!
asp.net asp.net-mvc-5
How do I handle requests to actions that exist, but don't support the http method in the request?
For example, I have the following action:
[HttpGet]
[Route("~/")]
[Route("ServiceVisits")]
public ActionResult Index()
{
return View();
}
If a GET
request comes to http://localhost:59949/
or http://localhost:59949/ServiceVisits
then we get the page back, but if a POST
request comes, then I get an exception in my Application_Error
method in Global.asax.cs
that I have to deal with.
Requests that come to non-existent actions will return a 404
page, because I have the following route:
routes.MapRoute(
"PageNotFound",
"{*.*}",
new {controller = "Error", action = "NotFound"}
);
I'm using Attribute Routing and the above convention is my only one for handling 404's.
I want to log only serious system errors in Application_Error
and not No matching action was found on controller
exceptions. Thank you!
asp.net asp.net-mvc-5
asp.net asp.net-mvc-5
asked Jan 3 at 19:41
Shahin DohanShahin Dohan
1,71711328
1,71711328
In theApplication_Error
you will get all sort of errors, regardless of how you are handling them. If your concern is simply not to log some specific errors, you could add a check in theApplication_Error
handler.
– Tasos K.
Jan 4 at 8:14
@TasosK. Is there no cleaner way of handling http method related 404's? I tried to design my application in a way that only critical server errors would reachApplication_Error
and everything else is handled by filters and such. Cheers!
– Shahin Dohan
Jan 4 at 18:49
add a comment |
In theApplication_Error
you will get all sort of errors, regardless of how you are handling them. If your concern is simply not to log some specific errors, you could add a check in theApplication_Error
handler.
– Tasos K.
Jan 4 at 8:14
@TasosK. Is there no cleaner way of handling http method related 404's? I tried to design my application in a way that only critical server errors would reachApplication_Error
and everything else is handled by filters and such. Cheers!
– Shahin Dohan
Jan 4 at 18:49
In the
Application_Error
you will get all sort of errors, regardless of how you are handling them. If your concern is simply not to log some specific errors, you could add a check in the Application_Error
handler.– Tasos K.
Jan 4 at 8:14
In the
Application_Error
you will get all sort of errors, regardless of how you are handling them. If your concern is simply not to log some specific errors, you could add a check in the Application_Error
handler.– Tasos K.
Jan 4 at 8:14
@TasosK. Is there no cleaner way of handling http method related 404's? I tried to design my application in a way that only critical server errors would reach
Application_Error
and everything else is handled by filters and such. Cheers!– Shahin Dohan
Jan 4 at 18:49
@TasosK. Is there no cleaner way of handling http method related 404's? I tried to design my application in a way that only critical server errors would reach
Application_Error
and everything else is handled by filters and such. Cheers!– Shahin Dohan
Jan 4 at 18:49
add a comment |
1 Answer
1
active
oldest
votes
In the end I decided to simply check for 404's in Application_Error
and avoid logging those errors, like so:
protected void Application_Error()
{
var ex = Server.GetLastError();
if (ex is HttpException httpEx)
{
var httpCode = httpEx.GetHttpCode();
Context.Response.StatusCode = httpCode;
if (httpCode != (int) HttpStatusCode.NotFound)
{
Logger.Error(ex, "Unhandled HttpException in Application_Error");
}
}
else
{
Context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
Logger.Fatal(ex, "Unhandled exception in Application_Error");
}
Context.Server.ClearError();
Context.Response.TrySkipIisCustomErrors = true;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54028776%2fhow-to-handle-unsupported-http-methods-on-actions-in-asp-net-mvc5%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In the end I decided to simply check for 404's in Application_Error
and avoid logging those errors, like so:
protected void Application_Error()
{
var ex = Server.GetLastError();
if (ex is HttpException httpEx)
{
var httpCode = httpEx.GetHttpCode();
Context.Response.StatusCode = httpCode;
if (httpCode != (int) HttpStatusCode.NotFound)
{
Logger.Error(ex, "Unhandled HttpException in Application_Error");
}
}
else
{
Context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
Logger.Fatal(ex, "Unhandled exception in Application_Error");
}
Context.Server.ClearError();
Context.Response.TrySkipIisCustomErrors = true;
}
add a comment |
In the end I decided to simply check for 404's in Application_Error
and avoid logging those errors, like so:
protected void Application_Error()
{
var ex = Server.GetLastError();
if (ex is HttpException httpEx)
{
var httpCode = httpEx.GetHttpCode();
Context.Response.StatusCode = httpCode;
if (httpCode != (int) HttpStatusCode.NotFound)
{
Logger.Error(ex, "Unhandled HttpException in Application_Error");
}
}
else
{
Context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
Logger.Fatal(ex, "Unhandled exception in Application_Error");
}
Context.Server.ClearError();
Context.Response.TrySkipIisCustomErrors = true;
}
add a comment |
In the end I decided to simply check for 404's in Application_Error
and avoid logging those errors, like so:
protected void Application_Error()
{
var ex = Server.GetLastError();
if (ex is HttpException httpEx)
{
var httpCode = httpEx.GetHttpCode();
Context.Response.StatusCode = httpCode;
if (httpCode != (int) HttpStatusCode.NotFound)
{
Logger.Error(ex, "Unhandled HttpException in Application_Error");
}
}
else
{
Context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
Logger.Fatal(ex, "Unhandled exception in Application_Error");
}
Context.Server.ClearError();
Context.Response.TrySkipIisCustomErrors = true;
}
In the end I decided to simply check for 404's in Application_Error
and avoid logging those errors, like so:
protected void Application_Error()
{
var ex = Server.GetLastError();
if (ex is HttpException httpEx)
{
var httpCode = httpEx.GetHttpCode();
Context.Response.StatusCode = httpCode;
if (httpCode != (int) HttpStatusCode.NotFound)
{
Logger.Error(ex, "Unhandled HttpException in Application_Error");
}
}
else
{
Context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
Logger.Fatal(ex, "Unhandled exception in Application_Error");
}
Context.Server.ClearError();
Context.Response.TrySkipIisCustomErrors = true;
}
edited Jan 6 at 12:11
answered Jan 6 at 10:42
Shahin DohanShahin Dohan
1,71711328
1,71711328
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54028776%2fhow-to-handle-unsupported-http-methods-on-actions-in-asp-net-mvc5%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
In the
Application_Error
you will get all sort of errors, regardless of how you are handling them. If your concern is simply not to log some specific errors, you could add a check in theApplication_Error
handler.– Tasos K.
Jan 4 at 8:14
@TasosK. Is there no cleaner way of handling http method related 404's? I tried to design my application in a way that only critical server errors would reach
Application_Error
and everything else is handled by filters and such. Cheers!– Shahin Dohan
Jan 4 at 18:49