aryaxt
aryaxt

Reputation: 77596

ASP.Net MVC - Create Custom Routing?

I'm trying to create a custom routing. Here is what I've tried but does not work, what am I doign wrong?

Expected Call:

MyWebsite/Friend/Respond/55/4

routes.MapRoute(
            name : "Friend",
            url : "Friend/Respond/{id}/{state}"
);

// This method is in a Controller Named FriendController
[HttpPost]
public ActionResult Respond(int id, int state)
{
   // Do stuff
}

ANSWER:

routes.MapRoute(
            name : "ExtraParameter",
            url : "{controller}/{action}/{id}/{state}",
            defaults : new { }
);

Upvotes: 0

Views: 741

Answers (2)

Ozan ÇAKİN
Ozan ÇAKİN

Reputation: 163

You can set id and state UrlParameter.Optional.

    routes.MapRoute(
    "Default", 
    "{controller}/{action}/{id}/{state}", 
    new { controller = "yourcontrollername", action = "youraction", id = UrlParameter.Optional, state = UrlParameter.Optional 
    });

Upvotes: 1

Paul Welbourne
Paul Welbourne

Reputation: 1073

Can you post an example ActionLink to trigger your route?

Have you set-up defaults for your route:

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

Specifically the third argument in MapRoute. You might need to set your id and state parameters as UrlParameter.Optional

Upvotes: 2

Related Questions