我将如何配置global.config,以便在MVC2根路径actionMethod? [英] How would I configure the global.config to allow for root path to actionMethod in MVC2?

查看:118
本文介绍了我将如何配置global.config,以便在MVC2根路径actionMethod?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体而言,默认的目录命名结构为[控制器​​] / [ActionMethod]导致像www.mysite.com/home/actionMethodName一个呈现的网址。

specifically, the default directory naming structure is [Controller]/[ActionMethod] resulting in a rendered url like www.mysite.com/home/actionMethodName.

如果我想简单地意味着控制器(在这个例子中,家),这样我可以得到一个url看起来像这样什么:www.mysite.com/actionMethodName

What if I want to simply imply the Controller (in this example, 'home') so that I can get a url that looks like this: www.mysite.com/actionMethodName.

我还没有看到这种配置的许多要求。我可以看到它是如何打破惯例,但我想有很多人谁需要根路径。

I haven't seen many requests for this kind of configuration. I can see how it breaks convention, but I would imagine that there are lots of people who need root pathing.

推荐答案

由于您打算删除网址的{控制器}元素,你可能需要获得更多的具体与您的其他网址,如:

Because you are planning to remove the {controller} element of the url, you may need to get a bit more specific with your other urls, e.g.:

routes.MapRoute("MyOtherControllerRoute", "Account/{action}", new { controller = "Account", action = "Index" });
routes.MapRoute("MyDefaultRoute", "{action}", new { controller = "Home", action = "Index" });

当路由表被查询,如果使用的网址,如 www.mysite.com/Account ,它将匹配第一路径,因为我们已经具体关于用于匹配的URL模式。如果我们那么做类似 www.mysite.com/DoSomething 将使用我们最后选​​择的默认路由,试图调用HomeController的类型DoSomething的动作。

When the route table is interrogated, if the url such as www.mysite.com/Account is used, it will match the first route, because we have been specific about the pattern used to match the url. If we then do something like www.mysite.com/DoSomething it will use the default route we've selected last, trying to invoke the DoSomething action on the HomeController type.

我已经发现了一些是很多的MVC开发者似乎假定该URL严格 {事} / {事} / {事} ,而它基本上可以是任何你喜欢的,比如,我可以有一个执行路由: www.mysite.com/my-weird-and-wonderful-url 我可以映射具体做法是:

Something which I've noticed is that a lot of MVC developers seems to assume that the url is strictly {something}/{something}/{something}, whereas it can essentially be anything you like, e.g, I can have a route that does: www.mysite.com/my-weird-and-wonderful-url which I could map specifically:

routes.MapRoute("Somewhere", "my-weird-and-wonderful-url", new { controller = "Whatever", action = "Whenever" });

希望有所帮助。

这篇关于我将如何配置global.config,以便在MVC2根路径actionMethod?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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