【问题标题】:ASP - custom routeASP - 自定义路由
【发布时间】:2015-12-01 20:10:29
【问题描述】:

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

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

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

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

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

感谢您的每一个回答!

【问题讨论】:

  • 您使用的是哪个版本的 aspnet mvc? mvc 5 或 mvc 6。我猜这是控制器而不是 Web Api
  • mvc 5 和它不是 web api

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


【解决方案1】:

临时回家路线:

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

【讨论】:

  • 你能给我举个例子如何去 TempControler 到 CheckParameter action (url)?
【解决方案2】:

新路线

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

【讨论】:

  • 我真的很想用 .不带 /
  • 尝试上述方法,将 / 替换为 .在控制器之后,我认为这也应该起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
相关资源
最近更新 更多