在 ASP .NET MVC 3 中重写 Url - 在链接中添加 *.html 后缀

发布于 2024-12-31 23:58:54 字数 521 浏览 2 评论 0原文

我正在创建简单的应用程序,某种投资组合。我听说链接中最好有 *.html 后缀,因为在 Google 索引时它会给我更好的 SEO 结果...

无论如何,有没有办法修改默认路由/重写 url,以便我的链接看起来像这样(我使用的是波兰语单词,以便访问者可以阅读):

domain.pl/index.html
domain.pl/kontakt.html
domain.pl/oferta.html
domain.pl/sklepy.html

这些链接会被转换为一个控制器(例如 HomeController),但是是来自 {0} 的 {0}。 html 链接,将用作操作 姓名?或者更好的是,我想将 {0} 从网址映射到英文操作名称,例如:

index.html = index action
kontakt.html = contact action
oferta.html = offer action
sklepy.html = shops action

I'm creating simple application, some kind of portfolio. I've heard that it's better to have a *.html suffix in links, as it will get me better SEO results when indexing by Google...

Anyway, is there a way to modify default routing / rewrite url so that my links look like this (I'm using polish words that they are readable for my visitors):

domain.pl/index.html
domain.pl/kontakt.html
domain.pl/oferta.html
domain.pl/sklepy.html

And these links are translated into one controller (like HomeController), but the {0}, from the {0}.html link, will be used as an action name? Or even better, I would like to map {0} from Url to english action names like:

index.html = index action
kontakt.html = contact action
oferta.html = offer action
sklepy.html = shops action

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

比忠 2025-01-07 23:58:54

不确定是否有更好的 SEO 结果,但添加后缀很简单,

        routes.MapRoute(
            "Default",
            "{action}.html",
            new { controller = "Home", action = "Index" }
        );

只需将 .html 后缀添加到操作参数占位符即可。

对于翻译,您可以使用
ActionNameAttribute

    [ActionName("kontakt")]
    public ActionResult Contact()
    {
        return View();
    }

结合上面的两个代码,您将得到domain.pl/kontakt.html 映射到 Home/Contact 操作。

Not sure about better SEO results, but adding suffix is simple as

        routes.MapRoute(
            "Default",
            "{action}.html",
            new { controller = "Home", action = "Index" }
        );

Just add .html suffix to action parameter placeholder.

For translation, you could use
ActionNameAttribute

    [ActionName("kontakt")]
    public ActionResult Contact()
    {
        return View();
    }

With both codes above combined, you get domain.pl/kontakt.html mapped to Home/Contact action.

蓝戈者 2025-01-07 23:58:54

对于翻译和后缀,您可以尝试使用 AttributeRouting
安装此软件包后,您无需在 Global.asax 中配置路由,控制器将如下所示:

[GET("index.html")]
public ActionResult Index()
{
    return View();
}

[GET("/any/url/path/kontakt.html")]
public ActionResult Contact()
{
    return View();
}

[GET("oferta.html")]
public ActionResult Offer()
{
    return View();
}

顺便说一句,如果您想删除每个属性上重复的 .html,您可以创建自己的属性扩展 GETAttribute 并附加 .html。如果您有很多页面需要配置,这将很有用。

For both the translation and suffix, you can try using AttributeRouting.
With this package installed, you don't need to configure routes in your Global.asax and the controllers will be like this:

[GET("index.html")]
public ActionResult Index()
{
    return View();
}

[GET("/any/url/path/kontakt.html")]
public ActionResult Contact()
{
    return View();
}

[GET("oferta.html")]
public ActionResult Offer()
{
    return View();
}

By the way, if you want to remove the duplicated .html on each attribute, you can create your own attribute that extends GETAttribute and append the .html. This would be useful if you have lots of pages to configure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文