如何添加页面标题的链接在asp.net mvc的? (URL生成) [英] How to add page title in url in asp.net mvc? (url generation)

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

问题描述

如何动态地创建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

有什么我可以使用谷歌关于这个主题的一些文章的关键字? (如何genererate和处理这类网址asp.net的MVC内部)

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)

有一些问题:
如何生成链接? (存储蛞蝓分贝?)
重定向或不若蛞蝓是不是规范?

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

编辑:显然,他们被称为蛞蝓

edit: apparently they are called slugs

推荐答案

您必须按如下方式使用的东西。

You have to use something as follows.

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生成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆