How to shorten the url to just the action parameter - asp.net-mvc-4

If a website with url "www.site.com", redirects to HomeController's Index action.
I have
www.site.com/area/controller/action/{nick}
I want the url
www.site.com/{nick}
do the same thing
How I do to create a route and where do I have to create it?
In the RouteConfig.cs or in the AreaRegistration.cs?

In your RouteConfig.cs, add following route after all routes.
routes.MapRoute
(
name: "Nick Route",
url: "{nick}",
defaults: new { area = "AreaName",
controller = "controllerName",
action = "Actionname",
nick = UrlParameter.Optional });

I know that this post is old but if someone is interested, he may find the solution here http://www.nikolay-angelov.eu/custom-url-shortener-in-asp-net-mvc-5/
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "URL",
url: "{shortURL}",
defaults: new { controller = "Home", action = "RedirectToLong", shortUrl = UrlParameter.Optional }
);
routes.MapRoute(
name: "Basic",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);

Related

Issue in GET route in asp.net mvc razor c#

I have 3 routes total in my code.
If I change the position of 1st with 2nd, then 2nd starts to work but 1st gives 404 error.
Am I doing anything wrong in below code?
This is GET route and works perfectly.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new {
controller = "Category",
action = "Index",
Category = UrlParameter.Optional
}
);
This is GET route and always give 404 error.
routes.MapRoute(
name: "Default1",
url: "{Category}",
defaults: new {
controller = "Product",
action = "Index",
Category = UrlParameter.Optional
}
);
This is POST route and works perfectly.
routes.MapRoute(
name: "Default_Without_Action",
url: "{controller}/{action}/{Category}",
defaults: new {
controller = "Product",
action = "GetProducts",
Category = UrlParameter.Optional
}
);
test if you put url: "{controller}" and see if this works
routes.MapRoute(
name: "Default1",
url: "{controller}",
defaults: new {
controller = "Product",
action = "Index",
Category = UrlParameter.Optional
}
);
I checked both routes and working fine.
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Route -1
routes.MapRoute(
name: "Default1",
url: "{Category}",
defaults: new { controller = "Product", action = "Index", Category = UrlParameter.Optional }
);
//Route - 2
routes.MapRoute(
name: "Default",
//url: "{controller}/{action}/{id}",
//defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
url: "{controller}/{action}/{Category}",
defaults: new { controller = "Category", action = "Index", Category = UrlParameter.Optional }
);
}
On initial/first request it matches the route-1 and calls the Index Action Method of Product Controller as shown in the picture below. Reason - We defined the defaults parameter for controller Products.
If we remove the defaults parameter from route 1 then on initial/first request route-2 URL pattern matches and Index Action method of Category Controller will be called, Reason - We defined the defaults parameter for controller Category.
Browser response with 404 error only when any request doesn't match with the registered URL pattern.

MVC 4 multi routes in route.config

I am using default route as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{cvg}/{AgencyID}/{AgentCode}",
defaults: new { AgentCode = UrlParameter.Optional, controller = "Login", action = "Index", cvg = UrlParameter.Optional, AgencyID = UrlParameter.Optional }
);
I need to set one route with only Agentcode. I have use as below. but failed to get result.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{cvg}/{AgencyID}/{AgentCode}",
defaults: new { AgentCode = UrlParameter.Optional, controller = "Login", action = "Index", cvg = UrlParameter.Optional, AgencyID = UrlParameter.Optional }
);
routes.MapRoute(
name: "Agency",
url: "{controller}/{action}/{AgentCode}",
defaults: new { AgentCode = UrlParameter.Optional, controller = "Login", action = "Index" }
);
}
Am I missing some?
I find my answer self as
"Make sure the Default route is at the BOTTOM of your listed route table. Order matters when it comes to ASP.NET MVC Routing tables.
The correct ordering is your 'most specific' route to your least specific route."

MapRoute with my extension

How to create right MapRoute with my extension? (e.x. "my").
This MapRoute is correct
routes.MapRoute(
name: "Default",
url: "{controller}.my/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But I want use controller.my?id.
This MapRoute is wrong
routes.MapRoute(
name: "",
url: "{controller}.my/{tid}",
defaults: new { controller = "Home", action = "Index", tid = UrlParameter.Optional }
);
I think that if you add parameters in MapRoute, you can't simply use them as query strings.
Remove those parameters which you want to use them as query strings from MapRoute, and simply use them in action methods:
routes.MapRoute(
name: "",
url: "{controller}.my",
defaults: new { controller = "Home", action = "Index" }
);
Action method:
public ActionResult Index(string tid) // or int tid ...
{
// ...
}
So you can access that by something like ~/Home.my?tid=someThing

setting custom path in routeconfig.cs in mvc 4

Below is my routeconfig.cs file in mvc4 application
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{bizId}",
defaults: new { controller = "Home", action = "Index", bizId = UrlParameter.Optional }
);
routes.MapRoute(
"Outlet",
"Outlet/{bizId}",
new { controller = "Home", action = "Index" },
new { bizId = UrlParameter.Optional }
);
}
When i run the application, i need to enter /Home/Index?bizId=1 or any Id after the localhost port to run my application. It is working fine. But, now as the second route.maproute, I want the url to show as for ex: localhost:49787/Outlet?bizId=1 but this doesnt work. Please help! Thanks in advance
got the fix:
routes.MapRoute(
name: "Outlet",
url: "Outlet/{bizId}",
defaults: new { controller = "Home", action = "Index", bizId = 1 }
);

ASP.NET MVC 4 Routes not working

Route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
my controller
public class HomeController : BaseController
{
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
My Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
Finally my request url
http://localhost:1234/Contact/
Error on browser
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.
Requested URL: /Contact/
Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.18033
What am I doing wrong?
Solution:
Custom route should take the precedence
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The Framework always tries to match the URL of the request to a route in the order of the Routes added to the RouteCollection
So you should put the custom routes before the default route,
//Custom route
routes.MapRoute(
name: "Contact",
url: "Contact",
defaults: new { controller = "Home", action = "Contact" }
);
//default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
You can use:
routes.MapRoute(
name: "Default",
url: "{*p}",
defaults: new { controller = "Home", action = "Index", p = UrlParameter.Optional }
);
The asterisk indicates that it's a catch-all route. Keep in mind that these routes are extremely greedy, make sure that this stays below any specific routes.
You could also add a route constraint to this route which can determine whether the page exists in the database or something.