ASP.NET MVC 4 Map Route URlL is not working - asp.net-mvc-4

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

Related

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 }
);
}

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 }
);

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

ASP.NET MVC Router

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

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 }
);