Make custom default page in asp.net mvc - asp.net-mvc-4

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

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 MVC4 Custom routing

I want to create simple blog engine. For fancy and clean url I'd like to use routing mechanism implemented in MVC4.
I added to RouteConfig.cs this lines:
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: "ArticleList",
url: "Articles/{category}/{page}",
defaults: new
{
controller = "Articles",
category = UrlParameter.Optional,
page = 1
});
}
}
And if I write in web browser url:
http://localhost:6666/Articles/SomeCategory/3
I want to move to this controller:
public class ArticlesController : ControllerBase<IHomeService>
{
public ActionResult Index(string category, int page = 0)
{
return View("~/Views/Article/Articles.cshtml");
}
}
with parameters category = "SomeCategory" and page = 1.
All I recieve is
Server Error in '/' Application.
The resource cannot be found.
What is wrong?
routes.MapRoute(
name: "ArticleList",
url: "{controller}/{category}/{page}",
defaults: new
{
category = UrlParameter.Optional,
page = 1,
action = "Index"
},
constraints: new
{
controller = "Articles"
}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
EDIT
I should have added this to the answer but I was in a hurry:
Register your custom routes first, the more custom the more priority they have.
In the example above using the constraints or hard-coding the route produces the same result. Constraints are more flexible because you can use regex to restrict the controllers/actions/parameters values that your route is for. For instance, if you add a new route that uses the /category/page pattern your can then modify the controller constraint accordingly:
constraints: new
{
controller = #"^(Articles|AnotherController)$"
}
The problem is, you have an optional parameter in the middle of your {controller}/{category}/{page} path. ASP.NET routing has problem with that, because if category is not provided, it has no way to detect that the category is not provided.
To enable attribute routing, call MapMvcAttributeRoutes during configuration. Following are the code snipped.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
}
In MVC5, we can combine attribute routing with convention-based routing. Following are the code snipped.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
It is very easy to make a URI parameter optional by adding a question mark to the route parameter. We can also specify a default value by using the form parameter=value.

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