MVC3 Controller Folder Will Not Display in URL

This is just an example that I cannot figure out how to make it work.

In my Control MVC3 folder, if I add a new folder called "Admin" and add a "News" controller with the "Index" action, you will get a server error when trying to open this URL (404):

http: // url / admin / news

Even when you type “/ Index” behind it, this will not work. How can you create a hierarchy that will result in similar URLs? To be clear, I want to create a URL like:

http: // url / folder1 / folder2 / controller / action

thanks

+7
source share
1 answer

It seems you are still thinking in thinking of “physical file paths” .. NET MVC uses the concept of routing, where you define routes that map to classes and controller actions. In simple words, you are not attached to the file, you map it to the class.

If you look in the global.asax file of your web project, you will see a method called RegisterRoutes (). This method links all available routes for your site that will be used to find the correct controller / action / parameter / template to execute.

Now, as I would recommend deciding what you need is to create an area. It looks like you want to have an administrative section on your website, so I would do the following:

Right-click your website project, select Add, select Area

enter image description here

Give your area a name, in this case "admin" will make sense

Now the solution developer will add the "area" administrator. Notice how it mimics the structure and layout of a standard project, only in its own folder.

enter image description here

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

Here is the url of the test results:

enter image description here

This solution is for simplicity. If you want to accept, if in the future you have to delve into the creation of your own routes in the RegisterRoutes () method, which I mentioned above. Routing is what you need to get, and I recommend doing it.

+16
source

All Articles