ASP NET MVC Route - Not Found - asp.net-mvc

I'm having problems when using this route:
routes.MapRoute("ProductIndex", "pr-{key}", new { controller = "Home", action = "Product" });
When the key contains 'pr-', the route doesn't work.
Example: http://.../pr-my-product-key-with-pr-key

Routes don't work the way you want them to work, but it's easily converted
change your route to:
routes.MapRoute(
"ProductIndex",
"pr/{key}",
new { controller = "Home", action = "Product" });
and use:
http://.../pr/my-product-key-with-pr-key
or, if you really want to use that "way" you need to override the Initialization method of your main controller are check the link with StartWith() and redirect to the proper Controller.

Related

How to have a date "folder" in an MVC Url

I want to change the site I'm working on so that when someone creates an article, the path would be "Articles/ViewArticle/2016-10-04/test" instead right now it's just "Articles/ViewArticle/test". When I implemented it, I got an instant 404.
I tried creating this route before & after the Default Route, but still no go:
routes.MapRoute(
name: "ArticlesDefault",
url: "{controller}/{action}/{date}/{id}",
defaults: new { controller = "Articles", action = "ViewArticle", date = UrlParameter.Optional, id = UrlParameter.Optional }
);
I should note that the "id" is actually stored in the database as [date]/ArticleTitle.
Would anyone be able to help?
Use attribute routing instead. By using attribute routing you can construct the routes using the values of parameters passed to action methods.
More information on attribute routing is here https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/
if you do not want to use attribute routing, you can use constraints parameter of MapRoute method
Edit:
You can try any one of following approach
Without using attribute routing. I assume that your Id is in the form of {date}/articleTitle
routes.MapRoute(
name: "ArticlesDefault",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Articles", action = "ViewArticle" },
constraints: new { id = “\\d+” });
Using attribute routing. Best approach will be to separate article date and article title into two distinct parameters. Then you can use the following code.
[Route("Articles/ViewArticle/{articleDate:datetime}/{articleTitle}")]
public ActionResult ViewArticle(string articleTitle, DateTime articleDate)
{
//your action code goes here
}

Issues in MVC 2 Routing controller Index Page

I have Created a new Controller called Consultants. Then I create action method Index()..
I gave route like the following,
routes.MapRouteLowercase(
"consultants",
"consultants/index",
new { controller = "Consultants", action = "Index" }
);
In view, ActionLink method is,
<%: Html.ActionLink("Consultant Home", "Index", "Consultants", null, new { title = "Back home" })%>
But it is not routing. It is showing Resource cannot be find
Please correct my issues...
It looks like you've created a custom RouteCollectionExtensions called MapRouteLowercase (or at least I'm not familiar with it). I'd test that to make sure it's working as you expect by changing your route to this:
routes.MapRoute(
"consultants",
"consultants/index",
new { controller = "Consultants", action = "Index" }
);
Otherwise, you may have another route map causing issues, so make sure that route config is at the very top of your routing. Order plays an important role in how the route engine determines the correct url when searching for patterns. So order from specific to general.
For example, if you did something like this, it would cause issues with your current route:
routes.MapRoute(
"dateRoute",
"consultants/{date}",
new { controller = "Consultants", action = "Dates", date = UrlParameter.Optional }
);

MVC 3 - ActionLink

I am using RegisterRoutes method in Global file to route user url to actual url.
routes.MapRoute("Blog",
"blog/dp/{id}",
new { controller ="Blog", action = "Details" });
As you might have guessed, Blog is the controller and Details is its action.
So the problem is that the following code does not generate my desire URL which has dp word in its url.
By the way, I don't want to change my Action Name.
#Html.ActionLink( "headline", "Details", "Blog", new { id="1200" }, null )
Thanks in advance ;)
You could use a RouteLink instead of using an ActionLink. In a RouteLink you can explicitly pass your route name:
#Html.RouteLink("headline", "Blog", new { controller = "Blog", action = "Details" })
More about RouteLink method can be found in the MSDN. Reordering routes can do the job but that's a rather fragile mechanism.
The ActionLink may be using a different route from what you are providing in the question. Do you have any routes declared before this one?
The routes are matched in the order that you provide them. So, for instance, if you had something like:
routes.MapRoute("Blog",
"blog/dp", // {id} is not provided in this one
new { controller ="Blog", action = "Details" });
declared before this route:
routes.MapRoute("Blog",
"blog/dp/{id}",
new { controller ="Blog", action = "Details" });
The first route would be matched first and you would see /blog/dp/?id=1200 instead of /blog/dp/1200.

ASP.NET MVC 3 remove /Home

I've created a new ASP.NET MVC 3 internet application in Visual Web Developer 2010 Express, and I have noticed that with this default template, the path localhost:port/Home shows the same content as localhost:port/
Is there a way to remove the /Home? I would only like localhost:port/ to be the landing page.
Both urls work because that's how the default route has been defined in Global.asax:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This means that all of /, /Home and /Home/Index will land to the HomeController/Index action. So in fact when you are request / it is the exact same action being executed.
You can modify it like so:
routes.MapRoute(
"Default",
"{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Of course by doing this the only controller and action you will ever be able to run in your application will be the HomeController and Index action. No other action or controller will be ever accessible as you don't provide any means in the url to specify them. So I would leave the default routes as is because they allow to handle 99% of the cases unless you have some specific requirements.
The other guys are correct. However they dont really tell you a way around it. One way to get better control of the routes is as follows
Do something like below in the Register routes method
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
var controllers = typeof(MvcApplication).Assembly.GetTypes().Where(t => !t.IsAbstract && t.Navigate(_ => _.BaseType).Any(_ => _ == typeof(Controller)));
foreach (var controller in controllers)
{
var actions = controller.GetMethods().Where(m => m.HasAttribute<RouteAttribute>()).Select(m => new { Method = m, Attribute = m.GetAttribute<RouteAttribute>() }).ToArray();
foreach (var action in actions.OrderBy(m => m.Attribute.Path.Count(c => c == '{')))
routes.MapRoute(string.Format("{0}.{1}", controller.Name, action.Method.Name), action.Attribute.Path.TrimStart('/'), new { controller = controller.Name.Replace("Controller", ""), action = action.Method.Name });
}
and then decorate your controller methods with a route attribute that defines exactly what the route should be. You have to make the route attribute yourself, it cna be pretty simple just an attribute with a string parameter . In this fashion you can set any controller method to have any route you like.

Default route for all extreme situations

In my routing I would like to have something like not found route handler.
For example I have created one mapping like
routes.MapRoute(
"default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id="" }
);
routes.MapRoute(
"Catchall",
"{*catchall}",
new { controller = "Home", action = "Lost" }
);
but when user inserts address something like /one/two/three/four/bla/bla it will be cached with the Catchall mapping.
But when user inserts something that should match with default mapping,
(like /one/two/ ,but this controller or action is not implemented)
I would want that Catchall mapping would accept this request,
because all other mappings failed. But instead of this I get an error.
Should I override some mapping handlers to catch the exception if controller or action getting an exception?
The issue here is that it's not the Route's responsibility to make sure that "one/two" maps to a file. That responsibility falls to the ViewEngine. Since "one/two" is a valid route, it will be chosen.
If you want to handle the error of an invalid route, what I would recommend you do is simply use the built in ErrorHandling "page" to display whatever message you would have done in the Catchall.
I don't think this is the best solution, but you could always be more specific on your routes:
routes.MapRoute(
"home and action",
"home/index/{id}",
new { controller = "Home", action = "Index", id="" }
);
... repeat for the rest of your actions ...
routes.MapRoute(
"article catch all",
"home/{article}",
new { controller = "Home", action = "ArticleSearcher", article="" }
);
This would attempt to match a direct action, and if no action is found, pass what would normally be the {action} part of the default route to the 'ArticleSearcher' action as an article string parameter.
The downside is having to explicitly create each controller/action route.

Resources