0

I define a new route in my asp.net project

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

and controller (lets call it TempController) that have 2 actions :

  1. Index that does not get any argument
  2. CheckParameter that have one argument - input (string)

How to create a route to TempController to action CheckParameter?

Thanks for every answer!

2
  • which version of aspnet mvc are you using? mvc 5 or mvc 6. I am guessing this for a controller not a Web Api Commented Dec 1, 2015 at 20:26
  • mvc 5 and its not web api Commented Dec 1, 2015 at 20:35

2 Answers 2

1

Home Route For Temp:

routes.MapRoute(
        name: "TempHome",
        url: "{controller}.{action}",
        defaults: new { controller = "Temp", action = "Index"}
    );

Check Route For Temp:

routes.MapRoute(
        name: "TempCheck",
        url: "{controller}.{action}/{id}",
        defaults: new { controller = "Temp", action = "CheckParameter", id = UrlParameter.Optional }
    );

usage: http://www.website.com/temp.checkparameter/id

or you can have this:

routes.MapRoute(
        name: "TempCheck",
        url: "CheckSomething/{id}",
        defaults: new { controller = "Temp", action = "CheckParameter", id = UrlParameter.Optional }
    );

usage for id=10: http://www.website.com/CheckSomething/10

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

1 Comment

Can you give me an example how to go to TempControler to CheckParameter action (url )?
0

New Route

routes.MapRoute(
        name: "TempCheck",
        url: "{controller}/{action}/{stringParam}",
        defaults: new { controller = "Temp", action="CheckParameter",stringParam= UrlParameter.Optional }
    );

TempController.cs

public ActionResult CheckParameter(string stringParam){

}

Invocation

localhost:9090/temp/CheckParameter/PassAnyString

If you do not wish to add a new route you can also try this

http://localhost:9090/temp/CheckParameter?stringParam=11
In a @Url.Action would be :

@Url.Action("CheckParameter","temp", new {stringParam=11});

2 Comments

I really want create my custom route with . not with /
try the above approach by replacing / with a . after controller I think that should work as well

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.