ASP - 自定义路线

时间:2015-12-01 20:10:29

标签: c# asp.net routing asp.net-mvc-routing

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

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

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

  1. 没有任何争论的索引
  2. 具有一个参数的CheckParameter - input(string)
  3. 如何创建到TempController的路由以执行CheckParameter?

    感谢您的每一个答案!

2 个答案:

答案 0 :(得分:1)

Temp的主要路线:

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

答案 1 :(得分:0)

新路线

public ActionResult CheckParameter(string stringParam){

}

TempController.cs

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

如果您不想添加新路线,也可以尝试此


    function getGeneralTimeFormat()
    {
        return 'Y-m-d H:i:s';
    }