مسیریابی (Routing) در ASP.NET MVC 5.x
نویسنده: مهدی ملائیان
تاریخ: ۱۳۹۵/۰۳/۱۸ ۹:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
localhost:123/{controller}/{action}/{id}
| Id | Action | Controller | URL |
| null | Index | HomeController | http://localhost/home |
| 123 | Index | HomeController | http://localhost/home/index/123 |
| null | About | HomeController | http://localhost/home/about |
| null | contact | HomeController | http://localhost/home/contact |
| null | Index | StudentController | http://localhost/student |
| 123 | Edit | StudentController | http://localhost/student/edit/123 |
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Student",
url: "students/{id}",
defaults: new { controller = "Student", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
); | Id | Action | Controller | URL |
| 123 | Index | StudentController | http://localhost/students/123 |
| 123 | Index | StudentController | http://localhost/students/index/123 |
| 123 | Index | StudentController | http://localhost/students/index/123 |
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Product",
url: "{Product}/{productid}",
defaults: new { controller = "Product", action = "Details" }
); public class ProductController : Controller
{
// GET: Product
public ActionResult Details(int prodcutId)
{
return View();
}
} /Product/24 /Product/4
/Product/apple /Product/kish
routes.MapRoute(
name: "Product",
url: "{Product}/{productid}",
defaults: new { controller = "Product", action = "Details" },
constraints: new { productid = @"\d+" }
); public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
var routes = RouteTable.Routes;
using (routes.GetWriteLock())
{
// routes.Clear();
// routes.IgnoreRoute(...);
// routes.MapRoute(...);
// routes.Insert(0, newRoute) --> new routes are being added to the end of the route table
//get last route (default). ** by convention, it is the standard route.
var defaultRoute = routes.Last();
routes.Remove(defaultRoute);
//add some new route for a cms page
routes.MapRoute(yadayada);
//add back default route
routes.Add(defaultRoute);
} public static void RegisterRoutes(RouteCollection routes)
{
using (routes.GetWriteLock())
{
var pages = Task.Run(async () => { return await _pageService.FindAllAsync(); }).Result;
routes.Clear();
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
pages.ToList().ForEach(page => routes.IgnoreRoute(url: page.Url));
routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = MVC.Home.Name,
action = MVC.Home.ActionNames.Index,
id = UrlParameter.Optional
},
namespaces: new[] { $"{typeof(RouteConfig).Namespace}.Controllers" });
var defaultRoute = routes.Last();
routes.Remove(defaultRoute);
// routes.MapRoute(yadayada);
routes.Add(defaultRoute);
}
} var pages =_pageService.FindAllAsync().GetAwaiter().GetResult();