2012-11-01 50 views
0

問題如何隱藏在URL(MVC3)

在我的項目,我決定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

反正是有隱藏標識的網址,使網址看起來是搜索引擎優化,但可用於控制器?

+0

然後部分名稱必須是唯一的。 – Wazan

+0

我無法在我的服務中使用GetSectionBySectionName(字符串標題)方法,因爲標題已本地化爲5種語言:然後我的方法會變得複雜。 – Roman

+0

沒有一個唯一的ID或字符串,現在降落到一個特定的頁面!只需檢查鏈接http://www.tudor-tea.com,我已經爲每個頁面生成了一個唯一的字符串... – Wazan

回答

1

您可以嘗試在web.config中使用urlMappings。指定類似如下:

<urlMappings enabled="true"> 
    <add url="~/somedirectory/" mappedUrl="~/somedirectory/1/"/> 
</urlMappings> 

雖然,我不認爲任何工作,除非每個部分都有它自己的唯一名稱。否則,你會有一些衝突的URL。

你也可以考慮做一些自定義的工作,以及使用IIS的重寫模塊:

http://www.iis.net/learn/extensions/url-rewrite-module/using-the-url-rewrite-module

我所在的公司使用此爲它的KB文章系統,它類似於你的情況下工作,它工作得很好。 (文件夾/ ID)

+0

如果我期待不同的參數而不是始終將1作爲參數,該怎麼辦? – Biki