2016-04-15 44 views
0

我越来越喜欢的网址http://localhost:49671/TestRoutes/Display?f=hi&i=2如何网址,而不是ID在mvc.net使用路由

只显示名字,我想它像http://localhost:49671/TestRoutes/Display/hi

我把它从指数的方法。

[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 
    }); 
+0

你的路由定义需要匹配参数名称 - “TestRoutes/Display/{s}/{i}”(只有最后一个参数可以是UrlParameter.Optional) –

+0

我无法正确编写它,我可以使用属性代替Maproute ? – Nil

+0

什么不起作用?它只需要是'routes.MapRoute(name:“Professional”,url:“TestRoutes/Display/{s}/{i}”,新的{controller =“TestRoutes”,action =“Display”});'。但是在你的'Index()'方法中,它需要'return RedirectToAction(“Display”,new {s =“hi”,i = 2});'(不'f) –

回答

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 
} 
+0

默认之前新:) – Nil

+0

懒惰剪切和粘贴:) –

+0

我没有有意做它,我想这是一个错误的点击,我甚至没有意识到我什么时候点击。非常抱歉。 – Nil

0

改变你作为 routes.MapRoute路线( “专业”,//路线名称 “{控制器}/{行动}/{名称}”,//用参数URL 新 { 控制器= “TestRoutes”, 行动= “显示” } //参数默认值 );

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

我可能需要id属性才能继续操作。但即使我删除它,我也无法看到URL中的任何更改。如果可能的话,你可以细化一下吗? – Nil

+0

然后在route url中添加一个属性route.MapRoute(“Professional”,“{controller}/{action}/{name}/{id}”, new {controller =“TestRoutes”, action =“显示“, id = UrlParameter.Optional},以及像公共ActionResult显示(字符串名称,INT?ID){动作去这里} – user3275644

0

你的行动中取出图路线代码: routes.MapRoute( “专业”,//路线名称 “{控制器}/{行动}/{ID}/{名}“,//带参数的URL new {controller =”TestRoutes“,action =”Display“,s = UrlParameter.Optional,i = UrlParameter.Optional }); 使用属性路由代码: {Route(“TestRoutes/{s}/{i?}”)] public ActionResult Display(string s,int?i) { return View(); }

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/

+0

感谢您的链接,但我没有得到您的解决方案。你的意思是我应该写diff控制器进行路由吗? – Nil

+0

不,** RouteConfig.cs **位于您的MVC项目的“App_Start”文件夹下。如果你想使用属性路由,你必须改变你的RouteConfig.cs内容和你的其他控制器,比如在我的文章中。我认为属性路由是比常规路由更灵活的解决方案。你可以轻松控制你的路线。 – kkakkurt

+0

属性路由不适合我(我无法使用它,我的意思是)我得到默认的网址与ID?等等 – Nil

相关问题