MVC3控制器文件夹将不会出现在URL [英] MVC3 Controller folder won't appear in URL

查看:86
本文介绍了MVC3控制器文件夹将不会出现在URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这只是我无法弄清楚如何得到它的工作的例子。

This is just an example which I can't figure out how to get it work.

在我MVC3控制器文件夹,如果我添加一个名为管理​​的新文件夹,并用行动指数添加控制器新闻,当您试图打开URL(404)得到了一个服务器错误:

In my MVC3 Controller folder, if I add a new folder called "Admin" and add a controller "News" with an action "Index", you get a server error when you try to open that url (404):

的http:// URL /管理/新闻

甚至当你输入/索引背后,它不会工作。如何让一个层次,这将导致类似的URL的?只是要清楚,我想创建网址如下:

Even when you type "/Index" behind it, it won't work. How can you make a hierarchy which will result in similar URL's? Just to be clear, I want to create URL's like:

<一个href=\"http://url/folder1/folder2/controller/action\">http://url/folder1/folder2/controller/action

感谢

推荐答案

您似乎在物理文件路径仍是心态思考。 .NET MVC使用路由,可以定义映射到控制器类和动作路线的概念。在平淡的话,你不会映射到一个文件,你映射到一个类。

You seem to be thinking in the mindset of "physical file paths" still. .NET MVC uses the concept of routing where you define routes that map to controller classes and actions. In plain words, you're not mapping to a file, you're mapping to a class.

如果您在Web项目的Global.asax文件看看,你会看到一个名为的RegisterRoutes()的方法。这种方法的电线所有为您的网站中提供的路线,将被用来找到正确的控制器/动作/参数/模式来执行的。

If you look in the global.asax file of your web project you will see a method named RegisterRoutes(). This method wires up all of the available routes for your site that will be utilized to find the correct controller/action/param/pattern to execute.

现在,我建议的方式解决,你要寻找的是通过创建一个区域。这听起来像你想有一个管理部分到您的网站,所以我会做到以下几点:

Now, the way I'd recommend solving what you're looking for would be by creating an area. It sounds like you want to have an administrative section to your website so I'd do the following:

右键点击你的网站项目,选择添加,选择区

Right click on your website project, select "Add", select "Area"

给您所在地区的名称,在这种情况下,管理是有意义的。

Give your area a name, in this case "Admin" would make sense

您的解决方案资源管理器现在已经添加了区域管理。注意它是如何模仿标准项目的结构和布局,只有在它自己的文件夹中。

You solution explorer will now have added the "area" Admin. Notice how it mimics the structure and layout of the standard project, only in it's own folder.

在新建立的行政区域添加一个控制器,并将其命名为新闻报
添加您的行为

Add a controller in the newly created administrative area and name it "News" Add your actions

下面是一个测试结果的URL这样做:

Here's a test result URL doing this:

此解决方案是为简单起见。如果你想利用如果再你将不得不钻研()方法在我谈到上述的RegisterRoutes创建自己的路线。路由是你应该得到的,所以我建议这样做扎实掌握。

This solution is for simplicity's sake. If you want to take if further you will have to delve into creating your own routes in the RegisterRoutes() method I spoke of above. Routing is something you should gain a solid grasp of so I recommend doing so.

这篇关于MVC3控制器文件夹将不会出现在URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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