ASP.NET MVC throws error when id is null

I have a public controller (the action cannot be hidden under any security attribute). The controller has an action that is also available publicly.

The following is a sample controller structure:

 public SomeController : Controller {


       [HttpGet] 
       public ActionResult show(int id){

       }

 }

The action has a field idthat is required. However, it throws an error that unnecessarily adds to the logs when someone enters the wrong URL without the required one id(or when the search engine bots get to the URL with the required one id).

What should be the perfect solution to solve this decision.

Possible solutions that come to my mind:

  • Nullable (int? id) , , .
  • 404

?

P.S: ( ) , .

.

+3
2

. URL- ( 404), ( , , barfs, ).

http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs

( global.asax.cx):

// order is important; more specific routes to less specific routes is what you want
//
// default index action, with no ID
routes.MapRoute(
    "Default_Index",
    "{controller}/",
    new {controller="Home", action="Index"}
 );

// default create action, with no ID
routes.MapRoute(
    "Default_Create",
    "{controller}/Create",
    new {controller="Home", action="Create"}
 );

// default action requiring an ID (such as EDIT)
routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new {controller="Home", action="Index"},
    new {id = @"\d+" }
 );
+2

404 , - , .

Nullable, , , 404.

, Index(int? id) , id null . -.

+1

All Articles