Add more routing to asp.net MVC Global.asax - asp.net

I have one map route like :
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
);
}
but I want to add more route URL , how can I do that ?

Just add another MapRoute()
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"SecondRoute",
"{controller}/{action}/{tags}",
new { controller = "Products", action = "Index", tags = "" }
);
I suggest you go through this excellent post on routing by The Gu.

Related

MVC 5.0 RegisterRoutes not working

Anyone know why this is not working, i have googled it but nothing seems to work. All i want to do is allow www.abcdefg.com/username to map to a profile page but it does not work and i can't find out why.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
, new { controller = #"(admin|help|profile|Settings)" } // Constraints
);
routes.MapRoute(
"Users",
"{username}",
new { controller = "Home", action = "Index", username = "" });
}
You must adding the default route as the last route
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Users",
"{username}",
new { controller = "Home", action = "Index", username = "" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
, new { controller = #"(admin|help|profile|Settings)" } // Constraints
);

Url Route asp.net

my Model is
public class Organization
{
public int ID { get; set; }
public string Url { get; set; }
}
and this is my routeConfig
routes.MapRoute(
"Url",
"{controller}/{action}/{Url}",
new { controller = "Organization", action = "PageContent", Url = "" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Is there a way to fix the routing so that both Url and ID would work?
for example ...Organization/details/{ID} and ...Organization/details/{Url} would both work
As both the routes seems same you can combine both and define one and in the action (or onActionExecuting) you can decide the behavior based ID or URL
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{Segment}",
defaults: new { controller = "Home", action = "Index", Segment = UrlParameter.Optional }
);
Or you can map the route with ID only and take URL as querystring.

Show custom url?

How to display custom url and not based on controller and action names?
My routing looks like
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "productfeature",
url: "cn/pendrive/18-gb",
defaults: new { controller = "Product", action = "Feature", id = UrlParameter.Optional }
);
}
}
and my link button looks like
<li>#Html.RouteLink("Product Feature", "productfeature")</li>
I get url as the Url mentioned in the code but i also 404 error.
http://localhost:9090/cn/pendrive/18-gb
How to go for static urls? Basically my aim is to no show controllers and action names in url.
Put your custom route before the default one:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "productfeature",
url: "cn/pendrive/18-gb",
defaults: new { controller = "Product", action = "Feature", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

How to route from a area to the default route

I have a front page and a CMS area with the following routes:
Default Front Page route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "SiteFactory.Site.Controllers" }
Administration route
context.MapRoute(
"Administration_default",
"administration/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I would like to route from my ContentController (inside the administration area) to the front page HomeController, like this:
[HttpPost]
[ValidateInput(false)]
public ActionResult Save(string content, string contentId, string pageId)
{
if (ModelState.IsValid)
{
//TODO: save content.
}
return RedirectToRoute("Default");
}
How can i do this?
just return RidirectToAction("Index", "Home");

ASP.net mvc 2 routing

class HomeController
{
public ActionResult Search(int[] sections, int categories)
{
return View();
}
}
i need url like
website.com/search/1,2,3/5
What route map should i use?
At present RegisterRoutes looks
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
use something like:
var ints = new int[] {1, 2, 3, 4, 5};
var result = string.Join(",", ints.Select(x => x.ToString()).ToArray());
to build the commaseparated string, and pass it in the route "Search/{csv}/{somevalue}"
I'd change the controller definition to handle the array:
public ActionResult Search(string sections, int categories)
{
// sectionsArray is int[].
var sectionsArray = sections.Split(',').Select(x => int.Parse(x)).ToArray();
return View();
}
then you can define the routing as usual: Home/{sections}/{categories}:
routes.MapRoute(
"Search", // Route name
"{Home}/{sections}/{categories}", // URL with parameters
new { controller = "Home", action = "Search" } // Parameter defaults
);
Notice that you have to add this on top of the default one.
Add search route, don't replace default one
Since you'll probably still be using default route with controller/action combination I suggest you do this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Search",
"search/{sections}/{section}", // rename these variables to what they actually are
new { controller = "Search", action = "Sections", sections = UrlParameter.Optional, section = UrlParameter.Optinal }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
But since it's hard to tell what requirements you're after here, maybe you're trying to use catch-all route parameter but not at the end of your URL request. If that's the case, you can check my code in blog post of such Route class that allows catch-all section anywhere in the URL and not just at the end as it is by default.

Resources