I need to make sure that when people goes to my domain eg. mydomain.com
they get referred to the home action in the home controller, eg mydomain.com/home/home.
I can't seem to make it work using the default document settings in the IIS.
I guess it has something to do with the global.asax but I haven't been successful in getting anything to work. I still just gets a 404 as it tried to find the view in /
In global.asax you need to set the correct default controller and action in your route.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You would change action to "Home" instead of "Index".
You can default the site to land at Home/Home based on your mapped routes in Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new {
controller = "Home", // default controller when none is specified
action = "Home", // default action when none is specified
id = UrlParameter.Optional
}, // Parameter defaults
new string[]{ "Web.Controllers" } // namespace
); // (helpful when you have separate views)
}
Make sure you have a Home method inside your HomeController
To get the default URL mapped to it, you need to edit your Global.asax.cs and change Index to Home. I.e.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Home", id = UrlParameter.Optional } // Parameter defaults
);
Related
I have a requirement in which I have to map the below url
/amer/us/en/ = Home controller
/amer/us/en/login/index = Home controller
/amer/us/en/confirmation = Confirmation controller
along with the regular default action.
Eg if user goes to
http:\\test.com --> http://test/home/index
http:\\test.com/amer/us/en/login/index --> http://test/home/index
http:\\test.com/amer/us/en/ --> http://test/home/index
I was looking into attribute routing and so I added the below code in HomeController
[RoutePrefix("amer/us/en/")]
[Route("{action=index}")]
public class HomeController : Controller
{
}
and I am getting this error
The route prefix 'amer/us/en/' on the controller named 'Home' cannot begin or end with a forward slash and also the default routing is not working now so http://test.com is not loading anything. Below is my default RouteConfig class.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Very new to MVC. Can someone tell me what I am doing wrong here.
Routing in MVC works either by defining your routes in the RouteConfig class or by attribute routing (or you can use Areas). Routing with RouteConfig works with the order you define the routes with. When a request comes, MVC will try your routes from top to bottom and execute the first one that it can match with the requested url. So the routing need in your example can be achieved by:
routes.MapRoute(
name: "RootLogin",
url: "amer/us/en/login/index/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "DefaultAmer",
url: "amer/us/en/{controller}/{action}{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
this will map login as a special route and all the other /amer/us/en/ routes will go to whatever controller comes after and whatever action of it. The last route, if the request does not start with /amer/us/en will perform the default behavior.
Looks like, however, you want to define /amer/us/en/ as an Area, so you might want to take a look into that as well.
I'm working on an ASP.NET MVC web application. By default, browsing to the root of a directory seems to call the controller's Index() method. Is there way to change which method is called by default here? I know I could probably name the method I want to call "Index" and it would likely work, but I'd like to know if there's a way to point the directory root to a method that I choose.
For example: mysite.com/MyDirectory/ will call Index(), which is effectively browsing to mysite.com/MyDirectory/Index. I'd like to change it so that mysite.com/MyDirectory/ calls Details, (or "browses" to mysite.com/MyDirectory/Details).
Just change the action in the default route. Something like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Details", id = UrlParameter.Optional } // Parameter defaults
);
You could specify the behavior in your routes. If you are using the latest version of MVC, that would be in your \App_Start\RouteConfig.cs
You would have something like:
routes.MapRoute(
name: "MyDirectory",
url: "MyDirectory",
defaults: new { controller = "MyDirectory", action = "Details" });
You would place this before your default route, as your route table acts kind of like a switch statement matching on the first route that it finds.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "MyDirectory",
url: "MyDirectory",
defaults: new { controller = "MyDirectory", action = "Details" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
I'm working with an MVC3 (razor) application. In the default sample program, how can a user get the login page first, then to allow registered users to enter into homepage?
I tried to create one but landed in bunch of errors.
in Global.asax file, RegisterRoutes method
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }
);
you just need to change controller and action for default route.
You have to modify the default action in the route config.
routes.MapRoute(
"Home",
"{controller}/{action}/{id}",
new { controller = "LoginController", action = "loginView", id = UrlParameter.Optional }
);
Add the following code in the Application_Start method in your Global.asax:
GlobalFilters.Filters.Add(new System.Web.Mvc.AuthorizeAttribute())
and decorate your login action with [AllowAnonymous] attribute
i have two folder under view folder. one is Home and that has index.aspx file
another folder in view folder called DashBoard and that has MyDash.aspx
my routing code look like in global.asax
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
);
routes.MapRoute(
"DashBoard", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "DashBoard", action = "MyDash", id = UrlParameter.Optional } // Parameter defaults
);
}
so when i type url like http://localhost:7221/ or http://localhost:7221/Home then index.aspx is being render from Home folder but when i type url like http://localhost:7221/DashBoard then page not found is coming but if i type like http://localhost:7221/DashBoard/MyDash then page is coming.
so what is wrong in my second routing code . why MyDash.aspx is not coming when i type url like http://localhost:7221/DashBoard. what is wrong?
what i need to change in my second routing code??
please have a look.....i am new in MVC. thanks
My UPDATE
when i change route entry in global.asax file then it started working.
can u please explain why....
routes.MapRoute(
"DashBoard",
"DashBoard/{action}/{id}",
new { controller = "DashBoard", action = "MyDash", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
can i write routing code this way
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "DashBoard", action = "MyDash", id = UrlParameter.Optional }
);
same pattern for two url....please discuss in detail. thanks
The route names (1st parameter) have no impact on what action/controller gets invoked.
Your 2 route patterns, however, (2nd paramters of routes.MapRoute) are identical :
"{controller}/{action}/{id}"
... so anything that would be matched by the 2nd pattern gets caught by the first pattern. Therefore they're all getting mapped by the first map definition.
http://localhost:7221/Home works because it matches the first pattern, and presumably, the Index action exists inside your Home controller.
http://localhost:7221/DashBoard/MyDash works because, even though it's getting matched by the 1st route, it overrides the default action/controller (Home/Index) by the route parameters passed in through the URL (DashBoard/MyDash).
http://localhost:7221/DashBoard doesn't work because it's getting picked up by the first route pattern, but you didn't pass in an action name, so it looks for the default -- Index -- which I'm guessing you haven't set up within the DashBoard controller.
UPDATE (how to fix the problem):
So if you want http://localhost:7221/DashBoard to map to Controller named DashBoard with an action named MyDash, while still allowing other patterns to be picked up by {controller}/{action}/{id} delete your 2nd route, and place this one as the 1st route:
routes.MapRoute(
"DashBoard",
"DashBoard/{action}/{id}",
new { controller = "DashBoard", action = "MyDash", id = UrlParameter.Optional }
);
This is a more specific route, so it needs to go before the catch-all {controller}/{action}/{id}. Nothing that doesn't start with /DashBoard will get picked up by it.
I would like to "show" a dynamic value to the base of the URL, so the url would be like this: host.com/SOME_VALUE/{area}/{controller}/{action}.
So, if a url without the first value (the dynamic value) is requested: host.com/{area}/{controller}/{action} the correct action should be called but when a view is rendered (or maybe when a redirect occurs) the correct url should be returned with the correct first value.
This solution would be useful only to show in the url a specified value the identifies the user logon, like the username or maybe the company name, or any other value related to the current session, this value won't be used to restricts access to the actions, so the both urls should be valid and call the same action on then same session:
host.com/{area}/{controller}/{action}
host.com/some_value/{area}/{controller}/{action}
Any suggestions ?
This is completely untested but i'd have thought it would work...
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"MyRoute", // Route name
"{somevalue}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Would it be possible to add an additional static value to the URL when the somevalue part is used? i.e. host.com/users/some_value/area/controller/action. It would make the route mapping simple as all you'd need is:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"LoggedInUsers", // Route name
"Users/{somevalue}/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}