【问题标题】:How to hide id in the url (MVC3)如何在 url 中隐藏 id (MVC3)
【发布时间】:2012-11-01 05:40:38
【问题描述】:

问题

在我的项目中,我决定使用 db 存储实体“Section”来实现自定义菜单提供程序。 因此该部分映射到以下模型:

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

现在我需要让我的 Url 看起来像这样(没有 id,只有部分名称):

host/sectionSeName

是否有隐藏 url 中的 ID 以使 url 看起来对 SEO 友好,但可用于控制器?

【问题讨论】:

  • 那么部分名称必须是唯一的。
  • 我无法在我的服务中使用 GetSectionBySectionName(string title) 方法,因为标题已本地化为 5 种语言:那我的方法会很复杂。
  • 没有唯一的 id 或字符串,无法登陆特定页面!只需检查链接tudor-tea.com,我已经为每个页面生成了一个唯一的字符串...

标签: asp.net-mvc parameters asp.net-mvc-routing


【解决方案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

我工作的公司用它来做知识库文章系统,和你的情况类似,效果很好。 (文件夹/ID)

【讨论】:

  • 如果我期望不同的参数而不是总是 1 作为参数怎么办?
猜你喜欢
  • 1970-01-01
  • 2015-04-12
  • 2021-01-06
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 2021-10-31
  • 2018-07-08
  • 1970-01-01
相关资源
最近更新 更多