Default page for MVC application - asp.net-mvc

As far as I know, which isn't a lot, there is no way to set a default page, like Default.aspx for an MVC 4 application, right? I am developing MVC apps in convert with developers still working with Web Forms, which allows them create a default page. In other words they have a Default.aspx page in their root folder and when the user enters http://www.example.com it will display that page. We have a requirement to display some standard stuff before allowing the user to continue in any of the apps. So would I create a Default.aspx or Default.cshtml file and use this as the initial display page then call another controller and action after the user agrees to the terms? What's the best way to do this?

In MVC, you can set default route in RouteConfig.cs as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When the user first visits your page using www.example.com, the user will be redirected to Home/Index where you can place your terms and conditions. But, this might not be always the case as the user can navigate to any URL and you need a mechanism whether a user has accepted the terms and conditions or not. I would suggest putting a cookie or session variable saying the user has accepted the terms. Check whether the cookie or session exists or not. Otherwise redirect to your terms and conditions page. On terms and conditions page, don't check any cookies. You can check that cookie using a number of ways:
1) Using ActionFilterAtribute and overriding OnActionExecuting
2) Using HttpModule
3) Overriding Application_BeginRequest() inside Global.asax.cs

Related

Remove "Home/Index" secondary route to home page

I have an ASP.net MVC 5 site. The home page is at http://mydomain.
However, there's also a second route to the home page - http://mydomain/home/index - which I think
This causes problems because it may be seen as duplicate content, and images are broken on this page.
How can I totally remove this route (so it goes to a 404, I guess?).
I've searched Google but can only find articles on removing Home from routes entirely - not what I need.
I'm using Attribute routing, and this is all that's in the RouteConfig.cs:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Enable Route Attributes in Controllers
routes.MapMvcAttributeRoutes();
// Fall through all routes
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The Home Index action has no attribute route on it (as you'd probably expect?). This /home/index route works even on newly generated MVC projects - which I think is a bad idea?
How can I do this?
Are there any problems with removing this route I may not have considered?
thx.
You can block unintended routes that you don't want by using IgnoreRoute().
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("Home");
routes.IgnoreRoute("Home/Index");
// Enable Route Attributes in Controllers
routes.MapMvcAttributeRoutes();
// Fall through all routes
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
However, if these URLs are already in the wild, you should instead setup a 301 redirect to the canonical URL you intended. The simplest way to do that is with the URL rewrite module.
This /home/index route works even on newly generated MVC projects - which I think is a bad idea?
I see this as more of a blessing in disguise. It is an advantage over any SEO competitor using MVC who doesn't do the extra work to remove these routes when you are the one who does.
This is not necessary.
The default route provides optional controller and action names. So if user does not put any name for controller and/or action in path (/Home/Index or /Home in this situation) asp.net will put the right values in application routing.
Whenever you use Url.Action or Url.Route functions it will produce the shortest link for you. So in your website there will be always http://mydomain produced for your root. And for example Category > Index action it will produce http://mydomain/category.
In your website bots will never get to duplicate content if your links are in this way. If you are writing your links manually write as short as you can or simply use Url.Action.
About the images there must be something different, because images are static files. just use "~/imagefolder/imagename.jpg" way to get them. "~" is important to start link from the root of application if you are making your application work on a subfolder in IIS.

Is there a simple way to map a single URL to another URL in ASP.NET MVC?

I have a working ASP.NET MVC application to which I would like to add a very simple URL which I would like to be redirected to another URL, without the Controller part of the path in it.
I.e. I want to be able to tell people to go to mySite.org/Hello and have it direct them to mySite.org/Whatever/WhateverElse?myfun=9, or whatever.
So I added a method in PublicController that redirects Hello to whatever, which works, except it requires me to tell people to go to mySite.org/PUBLIC/Hello, and we'd like to not have the extra word "public" in there.
Hours of confusing study later, I see that I could perhaps add a line in Global.asax.cs such as: routes.MapRoute(name: "Hello", url: "Public/Whatever"); ... or something... but that actually changes the way everything gets routed, and after trying various syntactical variations, nothing has seemed to just change one basic address to map to another basic address.
Is there a simple way to get mySite.org/Hello to map to another URL, without the virtual subdirectory for the public controller?
Create a custom route (it would need to be the first route in the table)
routes.MapRoute(
name: "Hello",
url: "Hello",
defaults: new { controller = "Whatever", action = "WhateverElse" }
);
then /Hello will redirect to /Whatever/WhateverElse

MapRoute in mixed web forms/MVC app

I have an older web-form app and now I am trying to add new pages using MVC.
It all seems to work just fine except one thing:
The application's default page (login.aspx) is a web form.
When user hit link www.mysite.com, instead of opening www.mysite.com/login.aspx, the site immediately goes to route specified in global.aspx as
routes.MapRoute("FrontLine", "{controller}/{action}/{id}",
new { controller = "FrontLine", action = "QuickView", id = "" });
So, the question is how to make login.aspx a default page?
Is it possible to do without converting login.aspx to a MVC view and adding corresponding controller?
Found a solution:
excute line:
routes.MapPageRoute("Default", "","~/Login.aspx")
before calling
routes.MapRoute

Strange route problem in ASP.NET MVC - default route not hit

I have this two routes currently in my application after decommenting out many other ones. Let me first explain that I have quite a big application already but have come to a problem where my application does not start at the root url anymore.
If I set starting page to default.aspx then webapp starts at (example) http://localhost:55421/Default.aspx. I don't want that. I want it without Default.aspx
So I went into app properties and removed Default.aspx as starting page - now it is blank field (just like in a sample new MVC app if you create it in VS 2008).
But now application does start at the required URL but issues an error:
"The incoming request does not match any route."
Also If I use route debugger it also misses all routes and catches it by catchall route.
I don't know how all of this is possible since as I said above I have two default routes configured at this time:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Pages", action = "Display", slug = "Default" }
);
Any help appreciated
Am I right in thinking you are trying to hit
http://server/{controller}/{action}/{id}
with
http://server/
If you are I think you need to provide a default for the last parameter {id}. You have a default for a parameter slug but without a default for {id} I don't think ASP.NET Routing can hit it.
If I'm right
http://server/Pages/Display
should also not hit the default route, because you are expecting id in Display?
HTH
Alex

How do you set the startup page for debugging in an ASP.NET MVC application?

How do you start debugging the application at the application root? For example: http://localhost:49742/
I'm always getting a page which doesn't exist, such as:
http://localhost:49742/Views/Home/About.aspx
Note that it would be OK to start at http://localhost:49742/Views/Home/About
Go to your project's properties and set the start page property.
Go to the project's Properties
Go to the Web tab
Select the Specific Page radio button
Type in the desired url in the Specific Page text box
While you can have a default page in the MVC project, the more conventional implementation for a default view would be to use a default controller, implememented in the global.asax, through the 'RegisterRoutes(...)' method. For instance if you wanted your Public\Home controller to be your default route/view, the code would be:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Public", action = "Home", id = UrlParameter.Optional } // Parameter defaults
);
}
For this to be functional, you are required to have have a set Start Page in the project.
This works for me under Specific Page for MVC:
/Home/Index
Update: Currently, I just use a forward slash in the "Specific Page" textbox, and it takes me to the home page as defined in the routing:
/
Selecting a specific page from Project properties does not solve my problem.
In MVC 4 open App_Start/RouteConfig.cs
For example, if you want to change startup page to Login:
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Account", action = "Login"} // Parameter defaults
);
If you want to start at the "application root" as you describe right click on the top level Default.aspx page and choose set as start page. Hit F5 and you're done.
If you want to start at a different controller action see Mark's answer.
Revisiting this page and I have more information to share with others.
Debugging environment (using Visual Studio)
1a) Stephen Walter's link to set the startup page on MVC using the project properties is only applicable when you are debugging your MVC application.
1b) Right mouse click on the .aspx page in Solution Explorer and select the "Set As Start Page" behaves the same.
Note: in both the above cases, the startup page setting is only recognised by your Visual Studio Development Server. It is not recognised by your deployed server.
Deployed environment
2a) To set the startup page, assuming that you have not change any of the default routings, change the content of /Views/Home/Index.aspx to do a "Server.Transfer" or a "Response.Redirect" to your desired page.
2b) Change your default routing in your global.asax.cs to your desired page.
Are there any other options that the readers are aware of? Which of the above (including your own option) would be your preferred solution (and please share with us why)?

Resources