JEV
JEV

Reputation: 2504

MVC5 Routing with two parameters in an Area

I have an area called News, and within that area I have a Post Controller. The post controller has the following action:

public ActionResult Index(int id, string name)
{
...
}

I have a route in the NewsAreaRegistration

    context.MapRoute(
        "News_post",
        "News/{controller}/{id}/{name}",
        new { action = "Index", controller = "Post",name = UrlParameter.Optional }, new { id = @"\d+"}
    );      

I have also tried without the name object attribute, with and without the id @"\d+ .. Thing is I have this on another site with the exact same setup, just so confused at why it's not working as expected.

Now firstly, the action will resolve:

http://example.com/News/Post/Index/3 

When I want it to resolve to

http://example.com/News/Post/3 

And then I also want this string parameter at the end so it should resolve to

http://example.com/News/Post/3/test-post

but instead resolves to

http://example.com/News/Post/Index/3?test-post

I am having a total nightmare with this routing stuff. I have tried to mess around with the Routing attributes but also have no luck there with areas... Any ideas guys?

Upvotes: 0

Views: 320

Answers (1)

JEV
JEV

Reputation: 2504

So I fixed part of the issue by making the Route look like this:

       context.MapRoute(
            "News",
            "News/Post/{id}/{name}",
            new { action = "Index", controller = "Post" },
            new { id = @"\d+" }
        );

So I removed the UrlParamter.Optional for the name argument. I need to really get my head around how some of these routes work. At times it is so easy, but then others I can't get it to do the simplest things.

Upvotes: 0

Related Questions