如何使用路由

时间:2016-04-15 05:28:21

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

我的网址为http://localhost:49671/TestRoutes/Display?f=hi&i=2

我希望它像http://localhost:49671/TestRoutes/Display/hi

我用Index方法调用它。

[HttpPost]
public ActionResult Index(int? e )
{
    //  return View("Display", new { f = "hi", i = 2 });
    return RedirectToAction("Display", new { f = "hi", i = 2 });
}

索引视图

@model Try.Models.TestRoutes
@using (Html.BeginForm())
{
    Model.e = 5 ; 
    <input type="submit" value="Create" class="btn btn-default" />
}

显示操作方法

// [Route("TestRoutes/{s}")]
public ActionResult Display(string s, int i)
{
    return View(); 
}

路由配置文件

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        "Professional", // Route name
        "{controller}/{action}/{id}/{name}", // URL with parameters
         new { controller = "TestRoutes", action = "Display", s = UrlParameter.Optional, i = UrlParameter.Optional
    });
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id =   UrlParameter.Optional
    });

5 个答案:

答案 0 :(得分:2)

您需要将路线定义更改为

routes.MapRoute(
    name: "Professional",
    url: "TestRoutes/Display/{s}/{i}",
    default: new { controller = "TestRoutes", action = "Display", i = UrlParameter.Optional }
);

以便占位符的名称与方法中的参数名称匹配。另请注意,只有最后一个参数可以标记为UrlParameter.Optional(否则RoutingEngine无法匹配段,并且值将作为查询字符串参数添加,而不是路由值)

然后,您需要更改控制器方法以匹配路径/方法参数

[HttpPost]
public ActionResult Index(int? e )
{
    return RedirectToAction("Display", new { s = "hi", i = 2 }); // s not f
}

答案 1 :(得分:0)

将您的路线更改为     routes.MapRoute(     “专业”,//路线名称     “{controller} / {action} / {name}”,//带参数的网址     新     {      controller =“TestRoutes”,      action =“显示”     } //参数默认值     );

和你的行动

public ActionResult Display(string name)
{
     //action goes here
}

答案 2 :(得分:0)

删除maproute代码:     routes.MapRoute(         &#34;专业&#34;,//路线名称         &#34; {controller} / {action} / {id} / {name}&#34;,//带参数的网址          new {controller =&#34; TestRoutes&#34;,action =&#34; Display&#34;,s = UrlParameter.Optional,i = UrlParameter.Optional     }); 使用属性路由代码:     〔路线(&#34; TestRoutes / {S} / {I}?&#34)]     public ActionResult Display(string s,int?i)     {         return View();     }

答案 3 :(得分:0)

您也可以尝试使用属性路由。您可以使用属性路由更轻松地控制路线。

首先更改你的RouteConfig.cs:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();
        //routes.MapRoute(
        //    name: "Default",
        //    url: "{controller}/{action}/{id}",
        //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        //);
    }
}

之后更改您的控制器文件:

namespace YourProjectName.Controllers
{
    [RoutePrefix("Home")]
    [Route("{action}/{id=0}")]
    public class HomeController : Controller
    {
        [Route("Index")]
        public ActionResult Index()
        {
            return View();
        }

        [Route("ChangeAddress/{addressID}")]
        public ActionResult ChangeAddress(int addressID)
        {
            //your codes to change address
        }
}

您还可以在此帖子中了解有关属性路由的更多信息: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/

答案 4 :(得分:0)

解决此问题的另一种方法是在默认路由之前放置正确的路由,如下所示:

routes.MapRoute(name: "MyRouteName", url: "Id", defaults: new { controller= "Home", action = "Index",id= Id });

默认路线:

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