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.
Related
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.
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.
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" }) %>
I'm trying to create a http://domain.com/NotAuthorized page.
went to Views\Shared and added a View called NotAuthorized witch originates the file name NotAuthorized.aspx
in my Routes I wrote
routes.MapRoute(
"NotAuthorized", // Route name
"NotAuthorized.aspx" // Route Url
);
but every time I access http://domain.com/NotAuthorized I get an error
The resource cannot be found.
What am I missing?
How can access this without using View("NotAuthorized") in the Controller, in other words, not passing through any controller.
You can't access views directly without passing through a controller. All pages in the Views folder cannot be served directly. So one way to accomplish what you are looking for is to write a custom[Authorize] attribute and set the error page:
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
base.HandleUnauthorizedRequest(filterContext);
}
else
{
filterContext.Result = new ViewResult { ViewName = "NotAuthorized" };
}
}
I still have no idea on how to accomplish it, but what I did was use the Home Controller and create an Action called NotAuthorized
public ActionResult NotAuthorized()
{
return View();
}
And add a route like
routes.MapRoute(
"NotAuthorized", // Route name
"NotAuthorized", // URL with parameters
new { controller = "Home", action = "NotAuthorized" } // Parameter defaults
);
And works fine now, I can easily redirect in any part of my Business Logic to /Notauthorized and that will route fine.
I create a new project and start it. It turns out that the controller Home is called with the action is default i.e. Index - this means Controllers/HomeController.Index() is called. The view displayed by this action is defined in this method - i.e. Views/Home/Index.apsx.
I want to change this starting behavior to be another controller and action. How can I do this?
Open the file Global.asax.cs and look for the line
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
There, change Home and Index to the your choice's values.
Hope that helps!