If you create a new asp.net mvc website the default routing in Global.asax.cs is as follows:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
This means that the following urls all resolve to the same page.
mysite.com
mysite.com/home
mysite.com/home/index
If anyone hits mysite.com/home or mysite.com/home/index I want the url to be mysite.com. This is the best url for it. Also it would mean that Google and other search engines don'think I have duplicate content.
How would I do this?
you could try two options:
Check out here for how to create URL rewrite rule.
You can also call for
context.Response.RedirectPermanently("mysite.com"); // you can do this in custom httphandler
If anyone hits mysite.com/home or mysite.com/home/index
not sure if this solves your Google search engine content duplicate.
This answer isn't specific to ASP.NET MVC, as you can use this on any page to confirm the correct canonical page for the content... for example, if you visit
www.stevefenton.co.uk/Content/Home/
You will see the following element in the head of the document...
<link rel="canonical" href="http://www.stevefenton.co.uk/">
This confirms to web crawlers that the master address for the page is in fact the root page.
To make this specific to your ASP.NET MVC page, your best bet is to ensure you always link to the correct variant of your address, so all links point to the correct place. Web crawlers follow your links (as do your users) so if your links are correct, they will index the content for those links.
Related
I am working on an Angular (6) app which sits within an existing MVC3 application. I.e. /MVC/App is my angular app. I have configured it using IIS Url Rewrite so that any request to "/Mvc/App" is forwarded to index.html page, just as described here, to make sure angular routing works:
https://blog.angularindepth.com/deploy-an-angular-application-to-iis-60a0897742e7
It all works, but it poses a requirement for existing clients that they will have to install IIS Rewrite Module as a prerequisite. So I am looking for an alternative and since we already have MVC3 I wanted to know if it is possible to achieve this using MVC3 routing.
Can we achieve what the IIS ReWrite is doing here to ensure that all requests are served up to the Index.html page with the child routes so that angular routing works as expected?
I achieved this by creating a controller with action and a routemap that maps any route starting with /Mvc/app to that action as below:
context.MapRoute(
"AngularAppRoute",
"app/{*pathInfo}",
new { controller = "SpaApp", action = "Index", pathInfo = "" }
);
I had to make sure that the Index action's view does not have any master page and it's content was set to the same as the Index.html file generated by ng-build.
Hello I am in desperate need of help. I just created an MVC site made in Visual Web Developer 2010
and each time and on different Web Hosts I get --Index of /-- instead of the site loading properly. It's as if the server or browser is looking for an 'index' file instead of being routed through the MVC folders to the proper start up page. I've done everything I believe I am suppose to do as far bring all my dll 'system' files to the 'bin' folder as well as everything else including getting a Web Host that has MVC supporting server but I still keep getting 'Index of /' and the folders instead of the site. Can anyone help? I'm really in a spot. I've been working on this site for months and I need to get it up and running.
Thanks, Rob
It looks like your default route is not setup or the setup is pointed to a place that doesn't exist. Your default route will be in the default.asax.cs page (MVC4 is App_Start/RouteConfig.cs) and looks something like this:
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index"} // Parameter defaults
);
So the the Default action is Home/Index. You can change this if you want to redirect to a different default page.
I'm writing an ajax application, I'm using the atribute allowanonymous for autentication ( http://blogs.msdn.com/b/rickandy/archive/2011/05/02/securing-your-asp-net-mvc-3-application.aspx )
today, as soon as anybody access the web application, it will redirect to ~/Account/LogOn and when the login is successful it will return RedirectToAction("Index", "Home"); and the browser URL will show: http:// website/ and from there I only use ajax calls, so the URL stays the same. (the content is called using json and the returned data is included in a main div)
I'm willing to make the URL be the root directory (http://website/) on the logon page as well. How can I accomplish it? I want the whole webapp to be in ajax, with no redirects. If I apply Ajax the way it is right now, the URL will stay as http:// website/Account/LogOn even after the login is already made.
I would suggest installing MVC 4, it has a default template that does exactly what you want, called a single page app. You don't have to use MVC4 for your app, but you can base your app on the same techniques. That way you can update to MVC4 when it is released with little issue.
MVC4 is currently in Beta, but is expected to be released this summer. MVC4 is also avaialable with a Go-Live license, so you can publish production sites with it.
It would also help if you used the right terminology. MVC has Action Methods and routes, not "directories". Routes are arbitrary and do not match to physical directories on the server.
I found a way to do, I edited my default route to:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
);
Web.config:
<authentication mode="Forms">
<forms loginUrl="~/" timeout="120" />
</authentication>
LogOn Method:
[AllowAnonymous]
public ActionResult LogOn()
{
if (User.Identity.IsAuthenticated)
{
return View("Home"); //Show welcome message
}
return View(); //Show logon form
}
This did the trick :)
I'm trying to deploy my finished website to the hosting company server and i'm basically stuck. I have wwwwroot folder on the server where I put my folder of the published project. So what is the index file, the first page that I should display.. is it the layout page or simply my index.cshtml or maybe the _ViewStart? I got to create a path to the folder where the file would be located and give them the name of the "index page". I've been trying to do this for a while now with no luck.
Your default route should go to the default controller/view (ie /home/index)
So if someone visits your site at www.yoursitewhatever.com it will automatically find the default page.
Here is a link that might help you and explain the differences between previous MVC versions .
http://haacked.com/archive/2011/05/25/bin-deploying-asp-net-mvc-3.aspx
A default project creates the default page in the global.asax file that is why by default it is located in home/index folder as in
routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults );
I have a subsite within my main site for site Administration. The site is physically stored in the form
~/Views/Administration/ViewName/Index
With controllers inside
~/Controllers/Admin/ControllerName
I am getting an exception trying to visit the page.
The view 'index' or its master was not found or no view engine
supports the searched locations. The following locations were
searched: ~/Views/ViewName/index.aspx ~/Views/ViewName/index.ascx
~/Views/Shared/index.aspx ~/Views/Shared/index.ascx
~/Views/ViewName/index.cshtml ~/Views/ViewName/index.vbhtml
~/Views/Shared/index.cshtml ~/Views/Shared/index.vbhtml
I added a route
routes.MapRoute(
"Administration", // Route name
"Administration/{controller}/{action}/{id}", // URL with parameters
new { controller = "Administration", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Going directly to the page manually
http://localhost:999/Administration/BaseItem/index
Does not result in exception, but I get no content. This leads me to believe it is not finding the View. What am I doing wrong?
I think the issue is that I have told the route engine how to route to the Controller, but I have not told the system how to route to the View. Where do I tell the system where the views are?
The problem is not with the routes, but with the design. The view engine cannot find your view, because it cannot find the correct path, since the default view engines are not designed to search for a subsite.
Instead of creating a subsite, make Administration an Area in your project. In AdministrationAreaRegistration.cs, you will set a route similar to the route you added. Place your views in the Views folder inside the Administration folder (inside the Area folder), and everything will work properly.