mersey
mersey

Reputation: 248

ASP - custom route

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!

Upvotes: 0

Views: 35

Answers (2)

Novice
Novice

Reputation: 478

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

Upvotes: 0

Ammar Hamidou
Ammar Hamidou

Reputation: 205

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

Upvotes: 1

Related Questions