本文介绍了如何在asp.net mvc的url中添加页面标题?(网址生成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何动态创建网址/链接,例如:www.restaurant.com/restaurant/restaurant-name-without-some-characters-like-space-coma-etc/132

How to dynamically create urls/links like:www.restaurant.com/restaurant/restaurant-name-without-some-characters-like-space-coma-etc/132

我可以使用哪些关键字来搜索有关此主题的文章?(如何在asp.net mvc中生成和处理这种url)

what are the keywords i can use to google some articles on this topic? (how to genererate and handle this kind of urls inside asp.net mvc)

有几个问题:如何生成链接?(将 slug 存储在 db 中?)如果 slug 不规范,是否重定向?

There are some questions:How to generate links? (store slugs in db?)Redirect or not if slug isn't canonical?

显然它们被称为 slug

edit: apparently they are called slugs

推荐答案

您必须使用以下内容.

Routes.MapRoute(
    "Post",
    "posts/{id}/{*title}",
    new { controller = "Posts", action = "view" }
);

还有一个简单的扩展方法:

And a simple extension method:

public static class UrlExtensions
{

    public static string ResolveSubjectForUrl(this HtmlHelper source, string subject)
    {
        return Regex.Replace(Regex.Replace(subject, "[^\w]", "-"), "[-]{2,}", "-");
    }

}

这篇关于如何在asp.net mvc的url中添加页面标题?(网址生成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-17 01:05