【问题标题】:Custome MVC Url Rout to display mixture of Id and UrlSlug自定义 MVC Url Routing 以显示 Id 和 Url Slug 的混合
【发布时间】:2013-04-23 14:12:33
【问题描述】:

嗨,我将像这样在我的博客网址中应用 Id 和 slug 的混合物作为网址

http://stackoverflow.com/questions/16286556/using-httpclient-to-log-in-to-hpps-server

为此,我在 global.asax 中定义了这个 Url

  routes.MapRoute("IdSlugRoute", "{controller}/{action}/{id}/{slug}",
                            new {controller = "Blog", action = "Post", id = UrlParameter.Optional,slug=""});

但是当我运行我的应用程序时,Url 看起来像这样:

http://localhost:1245/Blog/Post?postId=dd1140ce-ae5e-4003-8090-8d9fbe253e85&slug=finally-i-could-do-solve-it

我不想拥有那些?和 = 在网址中!我只想用斜线将它们分开 请问我该怎么办??

但是返回这个 Url 的 actionresult 的方式是这样的:

 public ActionResult Post(Guid postId,string slug)
        {
            var post = _blogRepository.GetPostById(postId);
            return View("Post",post);
        }

【问题讨论】:

  • 你怎么称呼这条路线?你能提供一个你的 ActionLink 等的例子吗?
  • 我刚刚编辑并添加了调用路由的actionreslust
  • 您是否像这样从 ActionLink 调用它...@Html.ActionLink("foo", "Post", new { controller = "Blog", postId = 1, slug = "foo-bar" })
  • @foreach(模型中的变量项){
    @Html.ActionLink(item.Title, "Post", "Blog", new { postId = item.Id, slug = item.UrlSlug }, null)
    }
  • 请不要将“ASP.NET MVC”简称为“MVC”。一种是框架,另一种是与语言无关的设计模式。这就像调用 IE - “互联网”

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


【解决方案1】:

确保您的自定义路由高于默认路由。它将在找到的第一个匹配路线处停止。

【讨论】:

    【解决方案2】:

    您是否尝试过将您的 postId 和 slug 都设置为 UrlParameter.Optionalroutes.MapRoute("IdSlugRoute", "{controller}/{action}/{postId}/{slug}", new {controller = "Blog", action = "Post", postId = UrlParameter.Optional,slug=UrlParameter.Optional});

    编辑

    我让这个在本地工作。我有一个模型:

    public class HomeViewModel
    {
        public Guid PostID { get; set; }
        public string Slug { get; set; }
    }
    

    一个有两个动作的控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Guid guid = Guid.NewGuid();
    
            var model = new HomeViewModel { PostID = guid, Slug = "this-is-a-test" };
            return View(model);
        }
    
        public ActionResult Post(Guid postID, string slug)
        {
            // get the post based on postID
        }
    }
    

    还有一个带有操作链接的视图:

    @model MvcApplication1.Models.HomeViewModel
    @{
        ViewBag.Title = "Home Page";
    }
    @Html.ActionLink("Click me!", "Post", new { postId = Model.PostID, slug = Model.Slug})
    

    为了让路由正常工作,我必须在默认路由之前对路由进行硬编码:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute("IdSlugRoute", "Home/Post/{postID}/{slug}",
                            new { controller = "Home", action = "Post", postID = Guid.Empty, slug = UrlParameter.Optional });
    
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    
    }
    

    【讨论】:

    • 是的,我只是测试了一下,我得到了同样可怕的 Url!
    【解决方案3】:

    更改您的路线以使用 postId 而不是 Id

    routes.MapRoute("IdSlugRoute", "{controller}/{action}/{postId}/{slug}",
                                new {controller = "Blog", action = "Post", postId = UrlParameter.Optional,slug=""});
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签