Understanding Default URL in MVC Routing - asp.net-mvc-routing

suppose if I have a route in my RouteConfig.cs file like this
routes.MapRoute(
"pattern1",
"{controller}/App{action}",
new { controller = "home", action = "index" }
);
and when I run my application I get an error HTTP Error 403.14 - Forbidden
but when I add this routes below like this
routes.MapRoute(
"pattern1",
"{controller}/App{action}",
new { controller = "home", action = "index" }
);
routes.MapRoute(
"pattern2",
"{controller}/{action}",
new { controller = "home", action = "index" }
);
then i am able to land to the Default page Home/Index.
My question is why am I not able to land to the default page when I have only one routing configured as shown in the first example.
I have configured the default controller to be Home and action to be Index, then to why am I getting error. Am I missing some concept of MVC routing technique.

Related

asp.net mvc6 / angular2 routing

I have asp.net mvc6 project. On client side i want to use angular2 to create single page application. It means i have two places where i can define routing.
As far it's SPA, routing has to be on angular2 side, but how to deal in case when user is refreshing page? MVC returns 404.
One way would be to set Startup route in mvc6:
routes.MapRoute(
name: "spa-fallback",
template: "{*url}",
defaults: new { controller = "OAuth", action = "Index" });
});
And then controller gives Request.Path to angular2.
However this {*url} does not recognize *.map and other static files.
Also there may be scenarious where i would like to use static page like Error.html etc.
Is there any better way to deal with routing on both (server/client) side?
I was facing the same issues and decided to use views instead of static files.
I have setup MVC with one Home controller that has 2 actions: Index and Login.
The app boostraps in the index.cshtml and the login.cshtml is a simple login page.
Routing in my startup.cs file is configured this way:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "login",
template: "login",
defaults: new { controller = "Home", action = "Login" });
routes.MapRoute(
name: "spa-fallback",
template: "{*url}",
defaults: new { controller = "Home", action = "Index" });
});
In my AppComponent I have (using Angular RC1):
#Routes([
{ path: '/Dashboard', component: Dashboard}
])
refreshing the page in http://localhost/Dashboard keeps me on the dashboard view, whereas http://localhost/Login redirects me to the login page, out of the angular app).
Hope this helps

I need to change my MVC2 home page to a different page

How can one go about doing that. I am betting it has something to do with the routing engine. Not sure though. I will continue to browse the interwebs...
You're right, you will want to change your default route. It is in the Global.asax.cs file (by default).
The default one looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
This line:
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
Is the defaults. The default controller name is Home and the default action is Index. Change them to what you desire.
You can learn more about routing here.
Look at your default route. Out of the box, it will point to the index method of the home controller.

MVC3 Razor routing from Area to Root

I have searched the site and cant find anything similar to my question. I am new to MVC so I may be asking a dumb question.
When the site is launched there are default Home and About buttons at the top. I added a new button that points to an Area, that works just fine.
When I hover over the Home and About buttons after I go to the View that is in my Area, they links on them show they are now pointing to my Area and not the root of my project.
How do I tell these buttons to still point to the root and not my Area?
Thanks for any help or words of wisdom someone will be providing.
This is supposed to take you to root folder.
#Html.RenderAction("Action", "Controller", new { area = "" });
Okay I forgot to ask you what the routes look like in your global.asax.cs file? I've got a similar setup, and here are my route definitions
routes.MapRoute(
"Home", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home",
action = "MaintainLists",
id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // defaults
);

Custom IRouteHandler route screws up MVC link building

I have a custom routehandler in ASP.NET MVC2 to catch all url's at a prefixed path like this:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new Route("#api/{*all}", new ApiHandler()));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Routing works fine, but if i use Html.Actionlink or return ReturnToAction() from a controller, the uri built creates a broken uri like this:
/#api?action=Add&controller=Home
instead of
/Home/Add
How can i influence the uri building logic to consider the Default route pattern?
I've come up with a hack to stop the described behavior. While this "solves" the problem, it's really kind of nasty and I'd prefer a cleaner way to exclude a route from the virtual path building logic
routes.Add(
new Route("#api/{*all}",
// A random and unlikely controller name as the default
new RouteValueDictionary() { { "controller", "qwewqewqeqweq" } },
// and a constraint requiring any controller to be that random, default value
new RouteValueDictionary() { { "controller", "qwewqewqeqweq" } },
new ApiHandler()
);
This means that the route would only be chosen for Virtual Path building if the controller in question was "qwewqewqeqweq", which hopefully is unlikely. I said it was nasty.
You could specify the name of the route using the RouteLink helper:
<%: Html.RouteLink("link text", "Default",
new { action = "add", controller = "home" }) %>

Custom route doesn't find controller action

For some reason my application isn't routing to my controller method correctly. I have a routelink like this in my webpage -
<%= Html.RouteLink("View", "Blog", new { id=(item.BlogId), slug=(item.Slug) }) %>
In global.asax.cs I have the following routes -
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"MoreBlogs",
"Blog/Page/{page}",
new { controller = "Blog", action = "Index" }
);
routes.MapRoute(
"Blog",
"Blog/View/{id}/{slug}",
new { controller = "Blog", action = "View"}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Blog", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And then I have a class BlogController that has a method -
public ActionResult View(int id, string slug)
{
... etc.
}
I put a breakpoint in the first line of the View method but it's not getting hit at all. I checked with a route debugger for the format localhost/Blog/View/1/test and it matched my custom route. All I'm getting is a 404 while running this, I can't work out why the route won't post to the view method in my controller - any ideas?
(Assuming the route debugger was based on Phil Haack's debugger post)
If you're getting a 404, that makes me think the actual view page itself cannot be found.
Assuming that you are also using areas within the application, and that RouteLink is actually being called from within an Area, I think you may need to specify area="" in your routeValues object ( you also need to specify the controller). In general, I think that you will need to add the area="..." part with all your Routelink calls when using Areas.
This is something that I have picked up about RouteLink and Area, but cant seem to find any reference material detailing the limitations.