ASP.NET MVC Router - asp.net-mvc-4

I have this routes:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Funcionario",
"{funcionario}",
new { controller = "Funcionario", action = "Index", funcionario = UrlParameter.Optional },
new string[] { "Route.Controllers" }
);
routes.MapRoute(
"Servico",
"{funcionario}/{servico}",
new { controller = "Funcionario", action = "Servico", funcionario = UrlParameter.Optional, servico = UrlParameter.Optional },
new string[] { "Route.Controllers" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = #"\d+" }, // Parameter defaults
new string[] { "Route.Controllers" }
);
}
But I can't access my home/index. For example:
Working
http://mydomain.com/pablo
http://mydomain.com/pablo/cozinha
http://mydomain.com/home/index/0
Not Working
http://mydomain.com/
http://mydomain.com/home/index

All the routes defined are generic.
http://mydomain.com/pablo will match the first route
http://mydomain.com/pablo/cozinha will match the second route
http://mydomain.com/ will match the first route (So home page will not be displayed)
http://mydomain.com/home/index will match the second route. (So home page will not be displayed)
Routes should be defined from specific to generic order. e.g. you can change the routes as,
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Servico",
"Func/{funcionario}/{servico}",
new { controller = "Funcionario", action = "Servico", funcionario = UrlParameter.Optional, servico = UrlParameter.Optional },
new string[] { "Route.Controllers" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = #"\d+" }, // Parameter defaults
new string[] { "Route.Controllers" }
);
}
So, your requests will be like,
http://mydomain.com/Func/pablo
http://mydomain.com/Func/pablo/cozinha
http://mydomain.com/
http://mydomain.com/home/index
So, routes should be declared carefully, so that request will not match any unwanted route

Related

Issue with default path in MVC

I am not able to set the default page in MVC. I am using lowercase-dashed-routing of git hub for url. When I open my project, I am getting 404 error. When I open page like localhost/home/index, it is working fine.
using LowercaseDashedRouting;
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
// );
routes.Add(new LowercaseDashedRoute("{controller}/{action}/{id}",
new RouteValueDictionary(
new { controller = "home", action = "Index", id = UrlParameter.Optional }),
new DashedRouteHandler() ) ); }}}

Routing in MVC is not working

I am reading an ebook on MVC.
It have following examples but none of them are working.
routes.MapRoute("", "X{controller}/{action}");
routes.MapRoute("", "Public/{controller}/{action}",
new { controller = "Home", action = "Index" });
Both of them will go to Home controller and index action
Link for First route: http://localhost:14099/XHome/Index
Link for Second route: http://localhost:14099/Public/Home/Index
The output of the both should be the same as per the example in the book. But when i'm trying the same it is not giving any result. i.e resource cannot be found. Am i doing anything wrong here?
Following is my code which i have written:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute("", "X{controller}/{action}");
routes.MapRoute("MyRoute", "{controller}/{action}",
new { controller = "Home", action = "Index" });
routes.MapRoute("", "Public/{controller}/{action}",
new { controller = "Home", action = "Index" });
}
I was writing the routes after default which is wrong, so i had to write it before the default which is quite obvious.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("", "X{controller}/{action}");
routes.MapRoute("MyRoute", "{controller}/{action}",
new { controller = "Home", action = "Index" });
routes.MapRoute("", "Public/{controller}/{action}",
new { controller = "Home", action = "Index" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

MVC RouteConfig, Route determinated controllers

I want access to my principal pages (index, about, contact...) only with {action}{id}, but to the others with {controller}{action}{id}. For example:
A principal page: myweb.com/index
No principal page: myweb.com/Account/Login
I've searched but I don't know how do it exactly.
This is my RouteConfig
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(
"Account",
"Account/{AccountId}",
new { action = "Index", controller = "Course" }
);*/
/*routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);*/
/*routes.MapRoute(
"Account",
"Account/{courseId}",
//new { action = "Index", controller = "Course" }
new { controller = "Home", action = "Index" }
);*/
//routes.Add()
}
}
Change your routes config as below but you must have all your principal actions in one controller say PrincipalController as you will not be having controller in your routes.
Always map the high priority routes before low precedance routes.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Principal",
url: "{action}/{id}",
defaults: new { controller = Princpal", action = "Index", id = UrlParameter.Optional }
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Please see example:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Principal pages have unique route per each page.
// Methods About and Contact should be implemented in HomeController.
routes.MapRoute(
name: "About",
url: "about",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Contact",
url: "contact",
defaults: new { controller = "Home", action = "Contact" }
);
// other pages use this default route
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

ASP.NET MVC 4 Map Route URlL is not working

I have following rout map.
routes.MapRoute(null, "{id}/{rent}/{unit}", new { controller = "Home", action = "Default" });
routes.MapRoute(null, "{id}", new { controller = "Home", action = "Default", id = UrlParameter.Optional });
routes.MapRoute(
"Default", //// Route name
"{controller}/{action}/{id}/{rent}/{unit}", //// URL with parameters
new { controller = "Home", action = "Default", id = UrlParameter.Optional },
new string[] { "CDCPortal" });
routes.MapRoute(
"DefaultRent", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Default", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "CDCPortal" });
and Below URLs are working fine.
localhost/118939/
localhost/118939/120/rent
localhost/Home/Default/118939/120/rent
but
localhost/Home/Default/118939
is not working properly. Am i missing something here?
What if you get rid of id = UrlParameter.Optional:
routes.MapRoute(
"Default", //// Route name
"{controller}/{action}/{id}/{rent}/{unit}", //// URL with parameters
new { controller = "Home", action = "Default" },
new string[] { "CDCPortal" });
Define DefaultRent before Default route

Make custom default page in asp.net mvc

I would like to add custom default page in Asp.net mvc so instead the page going to Home/Index , i would like to go to Account/Login. I have implement the following but it still go to Home/Index. Please advise what i did wrong. Thank you
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Custom",
url: "{controller}/{Account}/{page}",
defaults: new
{
category = UrlParameter.Optional,
page = 1,
action = "Login"
},
constraints: new
{
controller = "Account"
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Assuming the case that you want the users to be redirected to the login page or a custom page if they are not 'signed-in'.
You could create a filter attribute.
example:
[RequireHttps]
[AuthorizationFilter]
public class MyController : Controller
{
}
public class AuthorizationFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//do some validation, find if the user is signed-in.
filterContext.Result = new RedirectResult(..Some where in the site..);
}
}
Change your route. The default route is set to /Home/Index
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
You can change that to be any route you wish
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "ControllerName", action = "ControllerActionName",
id = UrlParameter.Optional }
);
replace your default route (the last one) with this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);