I have a action in a controller that looks like this
public ActionResult Transactions(string type) {}
To access this controller and pass in a type property value I have to type
www.mysite/controller/transactions?type=sometype
but what I want is to pass something like this
www.mysite.com/controller/transactions/sometype
So I create a route config param in the RouteConfig.cs file like this
routes.MapRoute(
name: "TransactionRoute",
url: "user/transactions/{type}",
defaults: new { controller = "user", action = "transactions", type = "made" },
constraints: new { title = @"^[A-Za-z]+$" }
);
but now if I pass a url like this
www.mysite.com/controller/transactions/made
the value of the string type in the action is null
Am I allowed to do this or did I do something wrong?
Here is my routeconfig.cs file
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "TransactionRoute", url: "user/transactions/{type}", defaults: new { controller = "user", action = "transactions", type = "made" }, constraints: new { title = @ "^[A-Za-z]+$" } ); routes.MapRoute( name: "RateRoute", url: "rate/event/{id}", defaults: new { controller = "rate", action = "event" }, constraints: new { id = @ "\\d+" } ); routes.MapRoute( name: "ReviewRoute", url: "rate/review/{id}", defaults: new { controller = "rate", action = "review" }, constraints: new { id = @ "\\d+" } ); routes.MapRoute( name: "SpaceCleanRoute", url: "space/{id}", defaults: new { controller = "space", action = "index", id = UrlParameter.Optional }, constraints: new { id = @ "\\d+" } ); routes.MapRoute( name: "SpacePendingRoute", url: "space/{id}/{pending}", defaults: new { controller = "space", action = "index", pending = UrlParameter.Optional }, constraints: new { id = @ "\\d+" } ); routes.MapRoute( name: "PublicSpaceRoute", url: "space/public/{title}", defaults: new { controller = "space", action = "public" }, constraints: new { title = @ "^[A-Za-z0-9-]+$" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }
www.mysite.com/user/transactions/sometype
should match for the TransactionRoute
.
Also I see no need for the title
constraint based on the route template.
Remove the title
constraint
routes.MapRoute(
name: "TransactionRoute",
url: "user/transactions/{type}",
defaults: new { controller = "user", action = "transactions", type = "made" }
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.