Asp.net MVC Routing


Standart home olarak gelen mvc sayfalarından sıkıldınız mı? Direk kopyala yapıştır yapabileceğiniz bir paylaşım vereceğim.

    //dosya yolu= App_Start/RouteConfig.cs
    public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                routes.MapRoute(
                    name: "Home",
                    url: "{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
            }

+Eğer attribute kullanmak isterseniz.

    public static void RegisterRoutes(RouteCollection routes)
           {
               routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
               routes.MapMvcAttributeRoutes();            
           }

Attribute kullanımı aşağıdaki gibidir.

    [Route("books")]
            public ActionResult Index()
            {
                return View();
            }
            [Route("books/{title}")]
            public ActionResult Index(string title)
            {
                ViewBag.Title = title;
                return View();
            }
            [Route("books/{title}/detail")]
            public ActionResult Detail(string title)
            {
     
                ViewBag.Title = title;
                return View();
            }

 

 

No comment

Bir cevap yazın

E-posta hesabınız yayımlanmayacak.