0

我有一个网址,上面写着 www.test.com/12345。当用户点击这个 url 时,他应该被重定向到相应的操作方法,其中 "12345" 接受作为其参数。

我有一个可以与 RouteConfig 一起使用的小想法,但仍然没有清晰的画面。

有人可以帮我吗?

动作方法是

[HttpPost]
public ActionResult DetailsByCode(string code)
{    
     IEnumerable<IProductListDto> prdctListDto = _productListDetails.GetProductListByCode(accessCode);
     Return "success";
}

并且 routeconfig 是

routes.MapRoute(
     name: "Accesscode",
     url: "{Areas}/{controller}/{action}/{id}",
     defaults: new { Areas = "Student", controller = "Student", action = "DetailsByCode", id = string.Empty }
 ); 

我完整的 routeconfig.cs 是

  public static void RegisterRoutes(RouteCollection routes)
    { 
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "DetailsByCodeRoute",
            url: "{Areas}/{controller}/{action}/{id}",
            defaults: new { Areas = "Student", controller = "Student", action = "DetailsByCode" }
          );

        routes.MapRoute(
            name: "Accesscode",
            url: "{Areas}/{controller}/{action}/{id}",
            defaults: new { Areas = "Student", controller = "Student", action = "DetailsByCode", id = string.Empty }
        );

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

        routes.MapRoute(
            name: "ChangePassword",
            url: "{Areas}/{controller}/{action}/{id}",
            defaults: new { Areas = "User", controller = "User", action = "ChangePassword", id = string.Empty }
        );
        routes.MapRoute(
           name: "PasswordReset",
           url: "{Areas}/{controller}/{action}/{id}",
           defaults: new { Areas = "User", controller = "User", action = "PasswordReset", id = string.Empty }
       );
4

2 回答 2

0

在您的情况下,您可以使用路由约束。

例如阅读这篇文章。 http://www.c-sharpcorner.com/UploadFile/ff2f08/custom-route-constraints-in-Asp-Net-mvc-5/

于 2016-01-18T06:37:16.017 回答
0

我没有添加新路由,而是在 Routeconfig.cs 中添加了一行用于启用属性路由。

routes.MapMvcAttributeRoutes();

和 action 方法之前的一行

[Route("{accesscode:int}")]
于 2016-01-21T10:42:53.027 回答