asp mvc在控制器名称和路由中使用破折号? [英] asp mvc using dashes in controller names and routing?

查看:111
本文介绍了asp mvc在控制器名称和路由中使用破折号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题。我对MVC相当新鲜,喜欢设置控制器和视图的新方式,但我无法弄清楚如何执行以下操作:



1)像www.homepage.com/coming-soon



为这种类型的网址做一个url什么是正确的方法?你创建一个名为ComingSoonController的控制器,不知何故地通过路由插入短划线?请注意,我不希望下划线,因为这不符合SEO的最佳利益。或者即将在某些其他控制器上的某些操作名称不在URL中并使用[ActionName(name-with-dash)]属性?



2)facebook,linkedin和twitter有网址,如www.facebook.com/[profile name]。在MVC中怎么做?显然,[profile name]是动态的。并且代码显然生活在称为配置文件的控制器中。所以在我看来,你必须使MVC足够聪明才能知道URL的第二部分是配置文件名称而不是控制器,并将其路由到配置文件控制器上的正确操作?这是比这更容易吗?

解决方案

我开发了一个开源的 NuGet库第一个将EveryMvc / Url转换为每个mvc / url的问题。



已破解的网址比SEO友善,易于阅读。 (我的博文上的更多内容



NuGet软件包: https://www.nuget.org/packages/LowercaseDashedRoute/



要安装它,只需在Visual Studio中打开NuGet窗口,方法是右键单击项目并选择NuGet软件包管理器,然后在在线选项卡上键入小写虚线路由,它应该弹出。



或者,您可以在包管理器控制台中运行此代码



Install-Package LowercaseDashedRoute



之后,您应该打开App_Start / RouteConfig .cs并注释掉现有的route.MapRoute(...)调用并添加它:

  routes.Add(new LowercaseDashedRoute ({controller} / {action} / {id},
new RouteValueDictionary(
new {controller =Home,action =Index,id = UrlParameter .Optional}),
new DashedRouteHandler()

);

就是这样。所有的url都是小写的,虚构的和隐式的转换,而无需再做任何事情。



开源项目Url: https://github.com/AtaS/lowercase-dashed-route



当它遇到第二个问题,您可以通过使用自己的路由或者使用自定义错误处理机制找不到方法来实现,但是如果限制配置文件url符合某些规则(如没有)任何斜线),以便您可以更容易地将其与其他网址区分开来,例如从内容文件URL即.css .js等。


i have two questions. i'm fairly new to MVC and love the new way the controller and views are set up, but i can't figure out how to do the following:

1) make a url like www.homepage.com/coming-soon

for this type of url what is the right way to do it? do you create a controller named ComingSoonController and somehow magically insert a dash via routing? note i do NOT want underscores as that's not in the best interest of SEO. or is coming-soon some action name on some other controller that is not in the URL and use the [ActionName("name-with-dash")] attribute?

2) facebook, linkedin and twitter have urls like www.facebook.com/[profile name]. how would this be done in MVC? obviously the [profile name] is dynamic. and the code would obviously live in a controller called, say, profiles. so it seems to me that you would have to make MVC smart enough to know when that second part of the URL is a profile name and NOT a controller, and route it to the right action on the profiles controller? is this easier than it sounds?

解决方案

I've developed an open source NuGet library for the first problem which implicitly converts EveryMvc/Url to every-mvc/url.

Dashed urls are much more SEO friendly and easier to read. (More on my blog post)

NuGet Package: https://www.nuget.org/packages/LowercaseDashedRoute/

To install it, simply open the NuGet window in the Visual Studio by right clicking the Project and selecting NuGet Package Manager, and on the "Online" tab type "Lowercase Dashed Route", and it should pop up.

Alternatively, you can run this code in the Package Manager Console:

Install-Package LowercaseDashedRoute

After that you should open App_Start/RouteConfig.cs and comment out existing route.MapRoute(...) call and add this instead:

routes.Add(new LowercaseDashedRoute("{controller}/{action}/{id}",
  new RouteValueDictionary(
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }),
    new DashedRouteHandler()
  )
);

That's it. All the urls are lowercase, dashed, and converted implicitly without you doing anything more.

Open Source Project Url: https://github.com/AtaS/lowercase-dashed-route

When it comes to the second problem, you can do this either by making your own routes or handling not found with a custom error handling mechanism, but the routing will be faster if you restrict the profile urls to concur to some rules (like don't have any slashes) so that you can distinguish it from other urls much more easily, like from content file urls i.e. .css .js etc.

这篇关于asp mvc在控制器名称和路由中使用破折号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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