0

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 { }
);

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Yes I have the default routing and here is the expected link: MyWebsite/Friend/Respond/55/4 There is no ActionLink I'm calling this service from an iOS client
Have you setup the defaults for your Friends route though? like so... routes.MapRoute( "Friend", "Friend/Respond/{id}/{state}", new { controller = "Friend", action = "Respond", id = UrlParameter.Optional, State = UrlParameter.Optional });
Setting parameters as optional was not required, but setting "controller, and "action" was. fixed the issue thanks. I was hoping based on the url it would detect them, but it doesn't
Actually I didn't have to do that either, Just changed it so that it's not specific to any controller
1

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 
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.