我希望使用MVC 4为我的博客建立一个SEO友好路线。我查看了几篇stackoverflow文章,并试图利用这些建议但是,我仍然无法让它正常工作。
我尝试做的是当有人点击其中一篇博文时,路线显示: blog / {blogCategory} / {blogTitle},例如“blog / cleaning / how-to-remove-stains”。我错过了一些重要的事情。我已经包含了控制器,视图和路线。我有两个问题:
为什么当我将blogTitle(“西班牙雨”)传递给控制器时,它带有连字符和空格?我希望能够获取标题并通过一个函数运行它以使其对URL友好并将其放回以显示在路径中。
为什么路线只能部分工作?它不显示blog / {blogCategory} / {blogTitle},而是显示blog / {blogCategory}?querystrings,请参阅下面的示例:blog / uncategorized /%20-rain%20in%20-spain%20-falls%20-mostly%20on %第二十条%20-平原?ID = 2。 (注意:未分类是其中一个类别)
ROUTE CONFIG:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.Canonicalize().NoWww().Pattern("([a-z0-9])([A-Z])", "$1-$2").Lowercase().NoTrailingSlash();
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Blog",
"Blog/{blogCategory}/{blogTitle}",
new { controller = "Blog", action = "Details" }
).RouteHandler = new Spotless_Interiors.Models._GlobalClasses.HyphenatedRouteHandler();
// Original route map
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).RouteHandler = new Spotless_Interiors.Models._GlobalClasses.HyphenatedRouteHandler();
}
}
查看:
@model Spotless_Interiors.Models.MultiModels
@{
ViewBag.Title = "Blog";
ViewBag.Separator = " | ";
ViewBag.Path = Request.Path;
}
@section menuLeft {
@Html.Partial("_BlogMenu")
}
@Html.Partial("_TitleBlockPartial", Model.Company)
@if (User.Identity.IsAuthenticated) {
<div class="blog-links">
@Html.ActionLink("Create New", "Create", "Blog", new { DT_Stamp = @DateTime.Now })
@ViewBag.Separator
<a href="@Url.Action("Meta-Tag-Editor", "Dashboard", new { pageName = (string)ViewBag.pageName, path = (string)ViewBag.Path })" >MetaTag</a>
<hr />
</div>
}
<div id="blog">
@foreach (var item in Model.PagedPosts)
{
<h1>@Html.ActionLink(item.Title, "Details", new { id = item.PostID, blogCategory = item.Category.CategoryName, blogTitle = item.Title }, new { @class ="blog-title" }) </h1>
<div class="full-width">
<div class="blog-category">
<h2>@Html.DisplayFor(modelItem => item.Category.CategoryName) | @Html.DisplayFor(modelItem => item.DT_Stamp)</h2>
</div>
<div class="blog-links">
@Html.ActionLink("Read More >>", "Details", new { id = item.PostID, blogCategory = item.Category.CategoryName, blogTitle = item.Title }, new { @class ="blog-links" })
</div>
</div>
<div class="full-width">
</div>
@Html.Raw(Server.HtmlDecode(item.TruncatedBody.Replace("<img ", "<img style = 'width:100px' ")))
<div class="ribbon" style="clear:both"></div>
}
</div>
控制器:
// GET: /Blog/Details
public ActionResult Details(int id = 0, string blogCategory = "", string blogTitle = "")
{
pageName = "Blog Details";
ViewBag.pageName = pageName;
var multiModels = _globalClasses.Standard(ViewBag.pageName, Request.Path);
///multiModels.Post = db.Posts.First(i => i.PostID == id);
multiModels.Post = db.Posts.First(i => i.PostID == id);
multiModels.PostComments = db.PostComments.Where(i => i.PostPostID == id).ToList();
blogTitle = _globalClasses.UrlFriendly(blogTitle);
if (multiModels.Post == null)
{
return HttpNotFound();
}
return View(multiModels);
}
答案 0 :(得分:0)
也许你的自定义扩展方法Canonicalize
(?)和自定义路由处理程序搞砸了。
关于ASP.NET MVC这已经足够了:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(name:"Blog", url:"Blog/{blogCategory}/{blogTitle}",
defaults:new {controller = "Blog", action = "Details"});
routes.MapRoute(name:"Default", url:"{controller}/{action}/{id}",
defaults:new {controller = "Home", action = "Index", id = UrlParameter.Optional});
}
和控制器:
public class BlogController : Controller
{
public ActionResult Details(string blogCategory, string blogTitle)
{
return Content(string.Format("category: {0}, title: {1}", blogCategory, blogTitle));
}
}
对于路径/blog/my%20category/my%20title
,您会得到正确的回复"category: my category, title: my title"
。