Custome MVC地址溃败显示Id和UrlSlug的混合物(Custome MVC Url Ro

2019-09-01 09:50发布

嗨“我要申请编号的混合物和蛞蝓的网址在我的博客网址是这样

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

要做到这一点我在Global.asax中定义以下链接

  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);
        }

Answer 1:

您是否尝试过设置你都和帖子ID蛞蝓是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
    }
}

并与ActionLink的一个观点:

@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
    );

}


Answer 2:

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



Answer 3:

改变你的路线,用帖子ID,而不是标识

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


文章来源: Custome MVC Url Rout to display mixture of Id and UrlSlug