Routing controllers in .Net MVC 3 - c#

I have created a folder admin inside controller folder and wrote few controllers in it. I want to access all functions in that controllers in that folder by a url like abc.com/admin//.
I can get it working directly when its put directly in controller folder.
EDIT
WHAT HAVE YOU TRIED?
Just created a folder and wrote normal controllers in it. But I don't know how to route it.

Well, i would suggest, rather than just creating a folder and create controllers inside of it. You should create an Area
When you create an Area in asp.net mvc, it will automatically create folders(controller, model etc) for you. And the routing setup as well.
For more info, please visit
http://www.codeguru.com/csharp/.net/net_asp/mvc/article.php/c20227/Using-Areas-in-ASPNET-MVC-Application.htm

Add a route for the admin controllers in your route config if you want to support that path in the URL. Something like:
routes.MapRoute(
name: "Admin",
url: "admin/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But #DotNetDreamer is right that Areas are a better solution for the admin functionality of your site.

Related

How does MVC routing detects file path

As we know that routing follows the routes provided in RegisterRoutes function in RouteConfig.cs which defines the controller and action in default routes.
My project only have default route like:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
So how does routing detects that whether we are accessing some action or pointing to a file like image, css or js.
The short answer is that any request for a resource that has a . in its name is directly served by IIS without invoking MVC routing. Conversely, requests that do not contain a . are passed to MVC for routing. Importantly this is just default behaviour.
As a quick test, fabricate a url for any resource that does not exist in your project such as \asdf and note the 404 error generated by MVC. Now repeat the test with \asdf.asdf and note the 404 error is generated by IIS.
It is possible to change this behaviour with directives in web.config. For example, see How do I route images using ASP.Net MVC routing?

How to add a page that print "Hello World" to an existing asp.net project?

I downloaded an ASP.NET project to my local computer via FTP and opened it in Visual Studio.
Currently the folder structure looks like following:
Can anyone please tell me how to add a new page which i can visit through domain.com/hello and prints "hello world" ?
I could not figure it out myself as I can't see any Controllers or Models folder. I also couldn't find how urls are being routed. When i visit domain.com/about I see the about page of the site which has its code in the following path: Views\Home\About.cshtml
But i'm not sure who is telling the site that /about should be routed to that file. I couldn't find any routes file.
You should create folder Controllers
Create Controller in folder with name CustomController
Create Action in controller Hello() which the return ur View "return View()"
Create folder in ur Views -> Custom
add Hello.cshtml in folder
go to App_Data
find your Routing File
And Add new Route for ur app
routes.MapRoute(
name: "hello",
url: "hello",
defaults: new
{
controller = "Custom",
action = "Hello"
}
);
Done

Resource cannot be found (MVC)

I moved a solution from one machine to another and am having nightmares. I've got one problem left. The default page can't get served, it says Resource cannot be found. I have in the routeConfig.cs
routes.MapRoute("Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
I get Resource cannot be found.
I can access the default page by :-
http://example.com/home/index
The other questions don't seem to answer it.
EDIT: If I put the above line of code at the top of the Routing code, the default page works but other pages don't. If I put the code at the bottom, other pages work but the default page doesn't.
Try look in properties there is "Web" tab were you cat chose server and project URL, hope
If you have only default routing on RouteConfig file in development machine and keep getting 404, it seems that your problem comes from project's virtual path settings.
http://forums.asp.net/t/1893154.aspx?The+resource+cannot+be+found+404
If application is deployed on local host (or you have set Use local IIS
Web Server) then it means you have created a virtual directory named
"YourSite".
Right click your app project on VS, choose Properties, then go to "Web" part and set your site's virtual path on "Project Url".
For virtual directory problem on live IIS server, see here to add virtual directory into destination machine before deploying your app.
In addition, if you have custom routing rule, make sure the custom route placed properly on top of default routing rule.
The problem was that I had two Config Rules pointing to the same address. One of the other rules was pointing to the wrong controller. The wrong controller was erroring because I had set up the action, I had used the name of the controller as the action, thats a no-no. The above rule was correct but the incorrect rule was
routes.MapRoute(
name: "pagelist",
url: "pagelist",
defaults: new { controller = "pagelist", action = "pagelist", id = UrlParameter.Optional }
);
the action should have been Index.

How do I create pages in a web api project?

I want to display a default status page for my web api project (where instead some IIS message is displayed when I start the project). However it seems like I cannot create views in web api (there is no support for ActionResult).
In addition to a status page I will also use this information to create an api documentation page.
How can I achieve displaying html pages in this situation ?
If your default status page is static html, you don't have to use MVC. Just tell WebApi in your Startup.cs that you want to support static resources:
app.UseFileServer();
For creating an API documentation, maybe you could write that file on startup dynamically?
You can create regular controllers and views in a webAPI project the same as any MVC project. Just create a normal controller that does not inherit from ApiController. In your startup.cs make sure to configure at least a default route.
configuration.Routes.MapHttpRoute(
name: "someName",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Right click and select Add, then you should see controller at the top. Select one of the mvc controllers.

How to configure ActionLink format to drop the subdirectory?

Let's say the domain that is mapped to my root hosting directory is example.com. GoDaddy forces mapping of other domains to subdirectories of the root. For example, my second domain, example1.com, is mapped to example.com/example1.
I uploaded my ASP.NET MVC site to such a subdirectory, only to find that ActionLinks that are for navigation have the following format:
http://example1.com/example1/Controller/Action
In other words, even when I use the domain that is mapped to the subdirectory, the subdirectory is still used in the URL. I want to change the format of my ActionLinks.
However, I noticed that I can also access the same path by going to:
http://example1.com/Controller/Action
(leaving out the subdirectory)
I want to have my ActionLinks automatically drop the subdirectory, as it is not required.
Is this possible without changing the ActionLinks into plain-old URLs?
No, I don't think so, as an action link mainly works to render the controller/action. The other work around, if you have install access for the server, is to use an URL Rewriting tool like iirf.codeplex.com, which is free and works pretty good. There may be some other unintended consequences with rewriting though, depending on what you are doing.
HTH.
You could try adding additional route statements in your global.asax, in order from your subdirectories. Example:
routes.AddRoute("example1/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.AddRoute("{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
I believe the routes are checked in order from the global asax, so you could effectively route the request to the right spot. However, your link would still contain the 'example1' folder in the URL.

Categories