Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET MVC路由。空白和类别_.net_Asp.net Mvc_Url - Fatal编程技术网

ASP.NET MVC路由。空白和类别

ASP.NET MVC路由。空白和类别,.net,asp.net-mvc,url,.net,Asp.net Mvc,Url,我有一个主导航,其中一个列表项呈现一个动作: [ChildActionOnly] public ActionResult BuildMenu(String category = null) { ViewBag.SelectedCategory = category; return View("~/Views/Article/CategoriesList.cshtml", this.GetItems()); } 其中GetItems方法为: [NonAction] public

我有一个主导航,其中一个列表项呈现一个动作:

[ChildActionOnly]
public ActionResult BuildMenu(String category = null) {
    ViewBag.SelectedCategory = category;
    return View("~/Views/Article/CategoriesList.cshtml", this.GetItems());
}
其中
GetItems
方法为:

[NonAction]
public IEnumerable<String> GetItems() {
    return this.session.Query<Article>()
        .Select(x => x.Category)
        .Distinct().ToList()
        .OrderBy(x => x);
}
如果我在这里应用类似于
category=Url.ToUrlFriendly(link)
(将所有不可接受的字符替换为破折号或任何其他字符),而在地址栏中它看起来很酷,那么我的控制器无法识别该类别(很明显,它与原来的不同):

公共操作结果索引(字符串类别,Int32?页){ //DB中没有这样的类别。。。 ViewBag.CurrentCategory=类别; 如果(类别=“全部”){ 返回视图(this.GetAllArticles().ToPagedList(第1页,this.PageSize)); } var entries=this.session.Query() .其中(c=>c.类别==类别) .OrderBy(d=>d.CreatedOn); 返回视图(entries.ToPagedList(第1页,this.PageSize)); } 我如何以最好的方式处理它?
谢谢

你也必须按相反的顺序来做。如果你对某些东西进行编码(比如用破折号替换空格),那么你必须解码(用空格替换破折号)。类似于Server.Encode和对立的Server.Decode。编写一些Url.DecodeUrlFriendly函数。但是,如果字符串已经包含破折号,请小心

我认为最好的办法是存储一个url友好的类别url部分,例如,对于
边缘区
,使用类似
边缘区
,它与类别信息一起存储。这样,它将在数据库和uri中的类别中保持不变


如果您有CMS,您可以将该url组件的生成作为CMS的一部分。

通常,您会将该类别的url友好名称作为数据库中的一个附加列,以便您可以查询该内容

@model IEnumerable<String>
@{ Layout = null; }

<ul class="transparent-custom">
@foreach(var link in Model) {
    <li>@Html.RouteLink(
        link, 
        new { 
            controller = "Article", action = "Index", 
            category = link
        },
        new {
            @class = link == ViewBag.SelectedCategory ? "selected" : ""
        }
    )
    </li>
}
</ul>
public ActionResult Index(String category, Int32? page) {

    // there's no such a category in DB...

    ViewBag.CurrentCategory = category;
    if(category == "All") {
        return View(this.GetAllArticles().ToPagedList(page ?? 1, this.PageSize));
    }
    var entries = this.session.Query<Article>()
        .Where(c => c.Category == category)
        .OrderBy(d => d.CreatedOn);
        return View(entries.ToPagedList(page ?? 1, this.PageSize));
    }