ASP - 自定义路线

ASP - custom route

我在我的 asp.net 项目中定义了一条新路线

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

和具有 2 个动作的控制器(我们称之为 TempController):

  1. 没有任何参数的索引
  2. CheckParameter 有一个参数 - 输入(字符串)

如何创建到 TempController 的路由以执行 CheckParameter 操作?

感谢您的每一个回答!

临时家庭路线:

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

检查临时路线:

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

用法:http://www.website.com/temp.checkparameter/id

或者你可以这样做:

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

id=10 的用法: http://www.website.com/CheckSomething/10

新路线

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

TempController.cs

public ActionResult CheckParameter(string stringParam){

}

调用

localhost:9090/temp/CheckParameter/PassAnyString

如果您不想添加新路线,也可以试试这个

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

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