ASP-自定义路由

本文关键字:路由 自定义 ASP- | 更新日期: 2023-09-27 18:25:12

我在asp.net项目中定义了一个新的路由

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

和控制器(让我们称之为TempController)有两个动作:

  1. 没有得到任何参数的索引
  2. 具有一个参数的CheckParameter-input(字符串)

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

谢谢你的每一个回答!

ASP-自定义路由

返回临时路线:

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