如何从 MVC5 中的 url 重定向



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

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

任何人都可以帮我吗?

操作方法是

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

和路由配置是

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

我的完整路由配置.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 }
       );

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

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

我没有

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

routes.MapMvcAttributeRoutes();

和操作方法前的一行

[Route("{accesscode:int}")]

相关内容

  • 没有找到相关文章

最新更新