MB34
MB34

Reputation: 4404

ASP.Net MVC4 routing

I have this method in my Home controller:

public ActionResult RequestExamReview(string EC)

I have this link in my view (in debug mode):

 http://localhost:50909/Home/RequestExamReview/201507LH123

But when I debug into the method and check EC, it is null.

What am I doing wrong? I'm using the default routing, do I need to create a new route definition:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Upvotes: 0

Views: 35

Answers (1)

Kevin Harker
Kevin Harker

Reputation: 406

You have a couple options:

  • You can name the parameter in your method "id" like it is defined in your route

    public ActionResult RequestExamReview(string id)

  • You can specify a new route that has the value ec as a parameter (but I wouldn't recommend this)

If you were to do that it would look something like this:

routes.MapRoute(
           name: "Default",
           url: "Home/RequestExamReview/{ec}",
           defaults: new { controller = "Home", action = "RequestExamReview" }
        );
  • You can use this url instead:

http://localhost:50909/Home/RequestExamReview?EC=201507LH123

  • You can use the Bind attribute to rename your parameter to id

public ActionResult RequestExamReview([Bind(Prefix="id")] string EC)

I wouldn't recommend adding a new route because it is best to keep the number of routes small to reduce complexity. You will find that as your app grows, managing the number of routes will become painful if you add a new route just to custom name your variables. In your case I would recommend just using the url with the query string values.

Upvotes: 3

Related Questions