I am working on a project, and I have some controllers there.Two of them is named "Class" and "Home" controller and the Class controller has a view named "Index" and the Home one has a view named "Index".But when I run the project in local first of all my Class/Index is shown and if I want to see the home page I should write Home/Index in the address bar.But it should show Home/Index first in running.What is the problem?!
In your RouteConfig.cs(App_Start) it should be like this,
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Note: Don't use the name of Class for custom codes. it is already defined by system.
Which controller is defined to run on your main root url, this all depends on your routing settings.
Please share the content of your Global.asax file to get more help on your problem.
Related
Is there a simple catch-all way to ensure only rewritten URLs can invoke a controller?
For example, if we have a URL www.somesite.com/about pointing to action "About" in controller "Shared", can it be ensured that any requests to www.somesite.com/shared/about end up at the rewritten URL, in this case www.somesite.com/about?
In other words, the user should not be able to just type /controller/action without being redirected to the rewritten URL.
However, we don't want to actively check and redirect but were hoping for some built-in function of MVC. The only suggestions I found along those lines were ChildActionOnly and HttpPost attributes, but they don't seem to be the answer (normal links don't work).
As mentioned, we're looking for something simple, more or less built-in - if it doesn't exist then so be it...
The built-in way of blocking routes is to use IgnoreRoute. It short-circuits routing and always makes the path throw a 404 not found.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Ignore /Home/About
routes.IgnoreRoute("Home/About");
// Register /About
routes.MapRoute(
name: "About",
url: "About",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Note that under the covers, it uses the StopRoutingHandler, which can be used (as a replacement for MvcRouteHandler) in any custom Route or RouteBase implementation to make more dynamic ignore rules than this.
NOTE: It is extremely important that IgnoreRoute is registered before the route you want to ignore in the route table.
I have an MVC website that uses Forms Authentication.
I am attempting to add a default landing page to my site ( landing.html ) that each user hitting
http://mywebsite.com for example will be redirected to.
I tried setting the default document in the web.config, but I always get redirected to the Account/Login page.
I do not want to change the login url because that will make the user's whose session has expired to go to the landing page, which is not what I want.
Any help would be appreciated.
Thanks
Update your "default" route. MVC projects have the following route defined in the RouteConfig class and is called in the Global.asax during application startup.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Just replace "Home" and "Index" with the controller and method you want to use as your default landing page.
Also be sure that if the controller has the [Authorize] attribute added to it then you need to add the [AllowAnonymous] attribute to the method you want to use.
Example : My User will enter www.xyz.com/Promo/PROMO123
where "PROMO123" is value, which i require.
above code produces error :
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
However
www.xyz.com/Promo/Index/PROMO123 will work properly,
but i dont want this.
How can i archive this
www.xyz.com/Promo/PROMO123
Have you tried Routing?
Such as
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Don't forget to add this before default one.
routes.MapRoute(
name: "PromoRoute",
url: "{controller}/{myString}",
defaults: new { controller = "Promo", action = "Index", myString = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
You need to define a route pattern for this.
If you want this to work application wide, then you will need to change the default route. But I would suggest simply adding a specific route for this controller, in addition to the default route, because you probably don't want to override the default MVC routing for the whole app or you will lose the ability to use multiple actions per controller.
See your RouteConfig, try this route (MVC pre-5):
routes.MapRoute("myRoute", "PromoRoute/{id}",
new {controller="PromoRoute", action = "Index"});
With MVC5 you can add this directly to your action assuming you've enabled attribute routes:
[Route("PromoRoute/{id}")]
public ActionResult Index(string id) {
}
I've got a legacy WebForms site with a Default.aspx page that's configured in IIS to be the default page. So when someone goes to mysite.com they see mysite.com/Default.aspx, but the url in the address bar only shows the mysite.com.
I added MVC to the site, and I want to gradually move functionality to MVC. Everything works, but it broke the root page: navigating to the route caused a 404 because the default route was matching the root and trying to route a the Home controller, but there wasn't one. So I added one that does this:
' GET: /Home
Function Index() As ActionResult
'jump to the go page
Return Redirect("/default.aspx")
End Function
This now works, except that when you navigate to the route, it shows mysite.com/Default.aspx in the address bar.
I'd like it if either:
I could NOT match the root route, and let the WebForms Default.aspx page handle it as before
I could route as I'm doing now, but make it so the Default.aspx page was not displayed in the address bar.
Are either of these possible?
It is possible.
In your route configuration, remove the controller in the defaults parameter.
From this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
To this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
Then add the Default.aspx to your project root
I need to have a parameter as part of my ASP MVC URL before Controller and Action:
http://www.mydomain.com/company1/Home
or
http://www.mydomain.com/company1/Clients/Detail/1
(Ideally I would like to have this as a sub-domain like this: http://company1.mydomain.com/Clients/Detail/1 so any answers solving this one is also appreciated)
I call this parameter Account. I tried adding something like this to the routing map:
"{account}/{controller}/{action}/{id}" but it gives me a 404 error when trying something like http://www.mydomain.com/company1/Home
Here is the RegisterRoutes in Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("TestRoute", "{account}/{controller}/{action}/{id}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
Is there anything special I have to do when organising my Views folder or Controller actions?
Your error sounds like you are not giving a default for action in your route defaults.