在我的項目,我決定imlement使用DB存儲實體「節」自定義菜單提供商ID。 所以部分被映射到以下型號:
public class TopMenuItemModel : BaseTrivitalModel
{
public TopMenuItemModel()
{
ChildItems = new List<TopMenuItemModel>();
}
public int ItemId { get; set; }
public string RouteUrl { get; set; }
public string Title { get; set; }
public string SeName { get; set; }
public IList<TopMenuItemModel> ChildItems { get; set; }
}
而對於模型視圖:
@model TopMenuModel
<nav id="main-nav">
<a href="@Url.RouteUrl("HomePage")">@T("HomePage")</a>
@foreach (var parentItem in Model.MenuItems)
{
<a href="@Url.RouteUrl("Section", new { seName = parentItem.SeName, sectionId = parentItem.ItemId })">@parentItem.Title</a>
}
</nav>
我的默認路徑是:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "Trivital.Web.Controllers" }
);
控制器菜單:
public class CommonController : BaseTrivitalController
{
...
public ActionResult TopMenu()
{
var sections = _sectionService.GetCollectionByParentId(0, true);
var model = new TopMenuModel();
model.MenuItems = sections.Select(x =>
{
var item = new TopMenuItemModel()
{
ItemId = x.Id,
Title = x.GetLocalized(s => s.Title, _workContext.WorkingLanguage.Id, true, true),
SeName = x.GetSeName(),
RouteUrl = "",
};
return item;
})
.ToList();
return PartialView(model);
}
}
}
現在我有一個SectionController在那裏我有一個ActionResult方法:
//section main page
public ActionResult Section(string seName)
{
var section = _sectionService.Get(1);
if (section == null || section.Deleted || !section.Published)
return RedirectToAction("Index", "Home");
//prepare the model
var model = PrepareSectionPageModel(section);
return View(model);
}
我現在的路線的部分(即給我的主機/ sectionSeName-ID):
routes.MapLocalizedRoute(
"section", // Route name
"{SeName}"+ "-" + "{sectionId}", // URL with parameters
new { controller = "Sections", action = "Section" },
new { sectionId = @"\d+" }
);
現在我需要讓我的網址看起來像這樣(沒有ID,只是部分名稱):
主機/ sectionSeName
反正是有隱藏標識的網址,使網址看起來是搜索引擎優化,但可用於控制器?
然後部分名稱必須是唯一的。 – Wazan
我無法在我的服務中使用GetSectionBySectionName(字符串標題)方法,因爲標題已本地化爲5種語言:然後我的方法會變得複雜。 – Roman
沒有一個唯一的ID或字符串,現在降落到一個特定的頁面!只需檢查鏈接http://www.tudor-tea.com,我已經爲每個頁面生成了一個唯一的字符串... – Wazan