How do I get rid of Home in ASP.Net MVC? - asp.net-mvc

I know this site is written using ASP.Net MVC and I do not see "/Home" in the url. This proves to me that it can be done. What special route and do I need?

Just change "Home" to an empty string.
routes.MapRoute(
"Home",
"",
new { action = Index, controller = Home }
);

If you're running on IIS 7, you can simply delete the Default.aspx file that comes with ASP.NET MVC (assuming you're running on Preview 3 or higher). That file was needed due to an issue with Cassini that was fixed in .NET 3.5 SP1. For more details check out:
http://haacked.com/archive/2008/04/10/upcoming-changes-in-routing.aspx
and
http://haacked.com/archive/2008/05/12/sp1-beta-and-its-effect-on-mvc.aspx

I actually like having all of my home controller methods to be at the root of the site. Like this: /about, /contact, etc. I guess I'm picky. I use a simple route constraint to do it. Here is my blog post with a code sample.

I'd add
routes.MapRoute("NoIndex", "{action}", new { controller = "Home", action = "Index" });
in RouteConfig.cs

This is what I did to get rid of Home. It will treat all routes with only one specifier as Home/Action and any with two as Controller/Action. The downside is now controller has to have an explicit index (/Controller != /Controller/Index), but it might help you or others.
routes.MapRoute(
"Default",
"{action}",
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
"Actions",
"{controller}/{action}",
new { }
);

In IIS 7, you can simply delete the Default.aspx file that comes with ASP.NET MVC (assuming you're running on Preview 3 or higher). That file was needed due to an issue with Cassini that was fixed in .NET 3.5 SP1.
For more details check out:
Upcoming Changes In Routing and .NET 3.5 SP1 Beta and Its Effect on MVC

Related

ASP .NET MVC RedirectoToAction ignores the application where is located

I have a MVC application deployed in my server in a virtual directory like:
http://localhost/myapp/
Where "myapp" is the virtual directory
In my Login view, located in
"http://localhost/myapp/user/login",
I redirect to the index using RedirectToAction("Index", "Home"), it seems the application try to redirect to
"http://localhost/home/index"
instead of
"http://localhost/myapp/home/index".
The application works when it's located in the root of the IIS Web Site but doesn't work in the given situation.
It's there a way to configure the application root, that I missed?
Settings: Microsoft Visual Studio Express 2012 for Web, IIS 7 under Windows 7 , application pool ASP .NET v4.0
I am 99% positive that doing:
return RedirectToAction("Index", "Home")
is application root relative meaning that it should redirect to the application you're in regardless of the virtual directory settings or where the application is located. I mean think of the nightmare otherwise that every time you move the app to a different virtual directory you need to update the global.asax or web.config file??? Ridiculous! We have the same setup that you have and we have no issues with "app hopping."
Are you sure that RedirectToAction is causing this? Could it be that you have something like:
#Url.Content("/Home/Index")
In that case you would experience this issue and you can easily fix this by doing:
#Url.Content("~/Home/Index")
~ symbol makes it application root relative...
thats correct functionality. MVC will calculate the route as controller/action by default.
If you want this to do otherwise you need to add a route into the Global.asax:
//this is your new route which needs to be ABOVE the detault
routes.MapRoute(
// Route name
"myapp_Default",
// Url with parameters
"myapp/{controller}/{action}/{id}",
// Parameter defaults
new { action = "index", id = UrlParameter.Optional });
//This is the default route that should already be there.
routes.MapRoute(
// Route name
"Default",
// Url with parameters
"{controller}/{action}/{id}",
// Parameter defaults
new { action = "index", id = UrlParameter.Optional });
more info on routing in scott gu's blog

Help with MVC view

When I create new MVC 3 project in Visual Studio the first thing I do is create a new controller called Home, and then right click in the Index stub and create a new view.
My question is when I have the Index file selected in the solution explorer and build the solution I get an error saying it can't be found. If I navigate to the page using root/ Home or root/Home/Index it still doesn't work.
I also created a new project using the sample website that ships with MVC and cannot figure out what code differs between an empty solution and the sample solution that could be giving me this problem. In the global.asax it looks like there is already a route setup for a home controller so I'm confused.
Your controller class should be called HomeController, not Home. On the screenshot you've shown I see that you've called it Home. By convention all controller classes in ASP.NET MVC must have the Controller suffix. In Global.asax you should have a default routing rule which states:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
which means that when you run the site and you request / it will look for the Index action on the HomeController.
I am unable to reproduce with Microsoft Web Developer 2010 Express, ASP.NET MVC 3 and creating a new empty Razor-view project. When you choose "Add View" did you enable "Create as a partial view"? If so, that may be the issue. Did you enable "Use a layout or master page" and select an existing master page? If not, that may be the issue...
The routing looks fine in an empty project. I see the Home/Index view with these URLs:
/
/home
/home/index
Post problematic project as .zip?

ASP.NET MVC2 Routing IIS6 - only default routes work

I've deployed an ASP.NET MVC2 website on a Windows Server 2003 machine running IIS 6.
I'm using pretty much the default routing in a standard MVC project:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Products", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
}
Navigating to http://localhost/MyApplication takes me to the List page just fine. Navigating to http://localhost/MyApplication/Products/Details/21 gives me a 404. This routing worked fine on the inbuilt development server in VS2010.
I've put in the standard IIS wildcard aspnet_isapi.dll mapping mentioned all over the place - navigating to the List page didn't work before I did - but navigating to anything other than the default route is broken.
I'd really like to keep my extensionless URLs. Does anyone have any idea as to why the routing would work for the default webpage, but no others?
*Edit: just tried adding the .aspx extension, ie now my route looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Downtime", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
And it has the same behaviour, except this time I get the asp 404 page instead of the html 404 page...
*Edit 2: tried it again using the following route and making sure .mvc was mapped to aspnet_isapi.dll:
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new { controller = "Downtime", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
This time I got an 'Internet Explorer cannot display the webpage' 404-style page. I've now got 3 different 404 errors from using these 3 different methods...
*Edit 3 Return of the Edit: I have the site running in IIS 5.1 on Windows XP professional with only a wildcard remapping on the virtual directory, but heaven forbid I can get it to run on the webserver...
First of all, you should follow this walk-through: http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx
(just to make sure you've not missed something). This shows how to get the extension-less and extention'd versions working. You might want to at least check whether the extension'd version works to check that other things aren't misconfigured.
After that, perhaps you should try adding something like this..
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
... as it can really help in determining where your routes are broken.
Some other things to check:
Is IIS configured with ASP.NET? Check properties of the virtual directory. On the VDir select the configuration button and check that the usual ASP.NET extensions are mapped to the .NET 2.0 ISAPI dll. This will be something along the lines of
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll.
Is ASP.NET 2 enabled on IIS (under IIS 6 this is off by default): in IIS manager check the Web Service Extensions folder to enable it.
Have you deployed global.asax?
Found it - the routing is actually working, my URLs were not. I was using javascript to build some of the URLs and it turns out that I wasn't linking to
http: //localhost/MyApplication/Controller/Action/ID
I was actually linking to
http: //localhost/Controller/Action/ID
Building the links like this was working on the development server, but once the site was deployed to a virtual directory on the webserver those addresses are incorrect because of the extra application name in the URL.
In conclusion, be careful with your URLs - don't build them out of strings like I did.

.net MVC 2 default Route suddenly stopped working

today the default route for my site stopped working, but the strange thing is that the global.ascx has not changed at all.
when i enter the URL mysite.com/
i get this 404 error
The resource cannot be found.
Requested URL: /Views/Start/Index.aspx
i have a bog standard default MVC route
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Index", action = "Index", id = "" }
);
the oddball thing is that even if i create a Start folder with a copy of my index view, it still doesn’t work and throws the same 404 error.
has anyone else had this issue ??
any help is most appreciated
Truegilly
Re-add ASP.Net MVC's default Default.aspx file to the site root.
This file forces requests to / to run through the routing engine.
Although not necessarily the answer to this question, when Haack's debugger is showing that your routing tables are set up fine but you're still getting 404s, check your Controller is public!

How to do routing for Phil Haacks area prototype for ASP.NET MVC 1.0?

I am building a simple web site for a client and this is my first time with ASP.Net Mvc.
For the production i need to use MVC 1.0 and the most efficient way to saparate Admin logic from the rest of this site is using Areas.
The fact that i couldnt use MVC 2, so i have used the Haacks area prototype and everything was ok.
I want to write a custom routing for paging results but i couldnt get it done.
routes.MapAreas("{controller}/{action}/{id}",
"Adore.Web",
new[] { "Admin" });
//my custom routing
routes.MapRoute(
"PagingServices",
"Admin/Services/{pageNumber}",
new { area = "Admin", controller = "Services", action = "Index" });
routes.MapRootArea("{controller}/{action}/{id}",
"Adore.Web",
new { controller = "Home", action = "Index", id = "" });
As you see above i am trying to get this "Admin/Services/1" but couldnt figure it out.
How can i do it, thanks in advance !
Have you tried Phil Haack's Routing Debugger? :) http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
That might help.
Also, it looks like the problem might be that your first route is going to match anything starting with /Admin. Try moving your custom route to the top since routes are evaluated in order.

Resources