Route Mapping not working with "area" - c#

I've been reading a lot about this problem, and I can't figure this out.
Everything is pretty straightforward with routing and ASP .NET MVC, but I'm stuck with this.
The problem is that I'm trying to make a GET to a given url with this form:
{area}/{controller}/{action}
But the {area} is not being registered. My default route is not working either (not working in the sense that I need to go to localhost:port/Home instead of just going to localhost:port/
This is my code:
RouteConfig:
public class RouteConfig
{
public static void RegisterRoute(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
route.MapRoute(
"Default",
"{controller}/{action}",
new {controller = "Home", action = "Index"}
);
}
}
This is the Area that is not being registered:
public class TransaccionesAreaRegistration : AreaRegistration
{
public override string AreaName
{
get{
return “Transacciones”;
}
}
public override void RegisterArea(AreaRegistrationContext context){
context.MapRoute(
"Transacciones_default",
"Transacciones/{controller}/{action}/{id}",
new { controller = "Transacciones", action = "Index", id = UrlParameter.Option}
);
}
}
Finally, this is my global.asax (I do call AreaRegistration.RegisterAllAreas() method):
protected void Application_Start(){
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
I will really appreciate some advice with this, I think I have spent enough time googling :O)

Just try this
RouteConfig:
public class RouteConfig
{
public static void RegisterRoute(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
route.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
This is the Area that is not being registered:
public class TransaccionesAreaRegistration : AreaRegistration
{
public override string AreaName
{
get{
return “Transacciones”;
}
}
public override void RegisterArea(AreaRegistrationContext context){
context.MapRoute(
“Transacciones_default”,
“Transacciones/{controller}/{action}/{id}”,
new { action = ”Index”, id = UrlParameter.Optional },
new string[] { "MyApp.Transacciones.Controllers" } // specify the new namespace
);
}
}
------------------------------OR Try This--------------------------------
public class RouteConfig
{
public static void RegisterRoute(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
AreaRegistration.RegisterAllAreas();
route.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

Reason why default route is not working
Because you never registered a default one. Add this line in the RouteConfig -
routes.MapRoute("Home", "", new { Controller = "Home", Action = "Index" });
So the final code should look like this -
public class RouteConfig
{
public static void RegisterRoute(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.MapRoute("Home", "", new { Controller = "Home", Action = "Index" });
route.MapRoute(
"Default",
"{controller}/{action}",
new {controller = "Home", action = "Index"}
);
}
}
Possible reason why Area seems not working
May be the same reason also the Area registration seems not working. Try adding the following line in area registration -
routes.MapRoute("Transacciones_Home", "Transacciones", new { Controller = "Transacciones", Action = "Index" });
So it looks like -
public class TransaccionesAreaRegistration : AreaRegistration
{
public override string AreaName
{
get{
return “Transacciones”;
}
}
public override void RegisterArea(AreaRegistrationContext context){
routes.MapRoute("Transacciones_Home", "Transacciones", new { Controller = "Transacciones", Action = "Index" });
context.MapRoute(
“Transacciones_default”,
“Transacciones/{controller}/{action}/{id}”,
new { controller = “Transacciones”, action = ”Index”, id = UrlParameter.Option}
);
}
}
}

This questions was the one that helped me.
The thing is, the order in the routes' registration is very important. Considering that, I started checking my other areas registration, and I found out that all the requests where falling into the first rule that was a general rule like this:
routes.MapRoute(
name : "Default",
url : {controller}{action}{id}
);
So, after that rule, none of the rules were being considered.
Thanks everyone for trying to help, bests!

Related

Constraints in route

So I have ASP.NET MVC application. I would like to configure its routes. Here is my RouteConfig's code:
public static void Register(RouteCollection routes, bool useAttributes = true)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
This route works fine. Besides I have an area in my application and try to configure its routes too. It is my area registration code:
public override void RegisterArea(AreaRegistrationContext context)
{
try
{
context.MapRoute(
name: "SiteSettings_Controller",
url: "SiteSettings/{controller}/{action}/{id}",
defaults: new {action = "Index", id = UrlParameter.Optional,
// here I tried to use #"(UserManagement|Tools|Settings)"
//as constraint but it takes no effect
constraints: new {controller = "UserManagement|Tools|Settings" }
);
}
catch (Exception e)
{
// here I get InvalidOperationException ""
}
}
I would like to restrict controllers in SiteSettingsArea's route but when I go to "localhost/SiteSettings/UserManagement" url I get InvalidOperationException with message "No route in the route table matches the supplied values". I believe that this url corresponds to SiteSettings_Controller route but obviously I am wrong. How could I limit controllers in the route properly?
If you search your codebase for SiteSettings_Controller does it appear anywhere else?
The below code certainly worked for me when I just tested it.
using System;
using System.Web.Mvc;
namespace WebApplication1.Areas.SiteSettings
{
public class SiteSettingsAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "SiteSettings";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "SiteSettings_Controller",
url: "SiteSettings/{controller}/{action}/{id}",
defaults: new
{
action = "Index",
id = UrlParameter.Optional
},
constraints: new { controller = "UserManagement|Tools|Settings" }
);
}
}
}

Dynamic routing action name in ASP.NET MVC

I would like to create a dynamic routing to a URL like following:
http://localhost:51577/Item/AnyActionName/Id
Please note that the controller name is static and doesn't need to be dynamic. On the other hand, I need to have the action name part dynamic so that whatever is written in that part of URL, I would redirect the user to the Index action inside of Item controller.
What I have tried so far is:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Items",
"Item/{action}/{id}",
new { controller = "Item", action = "Index", id = UrlParameter.Optional });
}
And when I build my app I get a following error:
The resource cannot be found.
Edit:
Here is my Global.asax file and the routeconfig.cs file:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
And here's the content of the RouteConfig.cs file with the answer that #Nkosi provided:
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: "Items",
url: "Item/{id}/{*slug}",
defaults: new { controller = "Item", action = "Index", slug = UrlParameter.Optional }
);
}
}
What you are referring to in your question is called a slug.
I answered a similar question here for web api
Web api - how to route using slugs?
With the slug at the end the route config would look like this
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Items",
url: "Item/{id}/{*slug}",
defaults: new { controller = "Item", action = "Index", slug = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
which could match an example controller action...
public class ItemController : Controller {
public ActionResult Index(int id, string slug = null) {
//...
}
}
the example URL...
"Item/31223512/Any-Item-Name"
would then have the parameters matched as follows...
id = 31223512
slug = "Any-Item-Name"
And because the slug is optional, the above URL will still be matched to
"Item/31223512"

route attribute not working mvc5

[RouteArea("Admin")]
[RoutePrefix("Post")]
public class PostController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
[Route("Create")]
public ActionResult Create()
{
var model = new Post();
return View(model);
}
If i'll try to access Index action it works but at Create action it doesn't. (It gives me HTTP 404)
I've added routes.MapMvcAttributeRoutes(); in RouteConfig.cs in App_Start folder
public class RouteConfig
{
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 }
);
}
}
but after that I'm getting an error
A route named “Admin_default” is already in the route collection.
After that I need to remove that line and delete all DLL in bin folder, than rebuild project.
other I've left as it is. What's wrong?
Of course route in area is correct
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}

Show views based on authentication status in asp.net mvc

If user is logged in I want to show my department view, if not logged in want to show login page. I have tried something like this inside my RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
if (HttpContext.Current.User==null)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
else
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Department", action = "Index", id = UrlParameter.Optional }
);
}
}
But this one always loads login page at startup.Can anyone point out what I am doing wrong here?
Note: I am using Asp.net Identity for this application
Your HttpContext.Current.User==null logic would go in the controller, not your route registration
Note- the correct call is Request.IsAuthenticated
Assuming you have an action method like this:
public ViewResult Index()
{
if(Request.IsAuthenticated)
return new RedirectResult("toLoginPage")
else
return new View("loggedInView");
}
However, I believe the [Authorize] attribute could be what you want in your use case: (note - having re-read the question, this may not be accurate, as you want to return a different view based on login status)
[Authorize]
public ViewResult ShowPerson(int id)
{
return new View("loggedInView");
}
And in your web.config, something like
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" />
</authentication>
</system.web>
In this particular instance, with the [Authorize] attribute above the action method, if the user is not logged in, they'd be redirected to log in.
Create your own Authorization attribute:
public class CustomAuthorize: AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if(filterContext.HttpContext.User.Identity.IsAuthenticated)
{
base.HandleUnauthorizedRequest(filterContext);
}
else
{
filterContext.Result = new RedirectToRouteResult(new
RouteValueDictionary(new{ controller = "Error", action = "AccessDenied" }));
}
}
}
Then add [CustomAuthorize] to your controller and change the route it points to.
This was taken from here
You can achieve this with route constraints:
public class DelegateConstraint : IRouteConstraint
{
private readonly Func<HttpContextBase, bool> _isMatch;
public DelegateConstraint(Func<HttpContextBase, bool> isMatch)
{
_isMatch = isMatch;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return _isMatch(httpContext);
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "CustomAuth1",
url: "AuthArea/{action}/{id}",
defaults: new { controller = "Department", action = "Index", id = UrlParameter.Optional },
constraints: new { auth = new DelegateConstraint(httpContext => !httpContext.Request.IsAuthenticated) }
);
routes.MapRoute(
name: "CustomAuth2",
url: "AuthArea/{action}/{id}",
defaults: new { controller = "Account", action = "Index", id = UrlParameter.Optional },
constraints: new { auth = new DelegateConstraint(httpContext => httpContext.Request.IsAuthenticated) }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
In this example the ~/AuthArea url will be resolved by Account or Department controller depending on the Request.IsAuthenticated property.
UPDATE:
This way you get a complete routing capability, but still need to specify the correct controller:
#Html.ActionLink("Context dependent link", "Index", #Request.IsAuthenticated ? "Account" : "Department")
This link would always be rendered as:
Context dependent link

Areas in asp.net mvc, only one work at time

My folder look like this:
(root)/Areas/Admin/Views/..
(root)/Areas/Admin/Controllers/...
(root)/Areas/Admin/Routes.cs
(root)/Areas/Forum/Views/..
(root)/Areas/Forum/Controllers/...
(root)/Areas/Forum/Routes.cs
public class Routes : AreaRegistration
{
public override string AreaName
{
get { return "Admin"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_Default",
"{controller}/{action}/{Id}",
new { controller = "Admin", action = "Index", Id = (string)null }
);
}
}
public class Routes : AreaRegistration
{
public override string AreaName
{
get { return "Forum"; }
}
public override void RegisterArea(AreaRegistrationContext routes)
{
routes.MapRoute(
"Forum_Default",
"{controller}/{action}",
new { controller = "Forum", action = "Index"}
);
}
}
Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
The startpage should be Home/Index but it start with Admin/Index, why?
Only site.com/Admin works not site.com/Forum
How should i get Admin and Forum Areas to work right? Why is only Admin working and not Forum?
When i delete Admin/Routes.cs file Forum start to work...
EDIT:
Home in ~/Views/ don't show as startpage even if i have
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
in my Global.asax after AreaRegistration.RegisterAllAreas();
I believe your area mappings should be structured like so.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_Default",
"Admin/{controller}/{action}/{Id}",
new { controller = "Admin", action = "Index", Id = (string)null }
);
}
and
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Forum_Default",
"Forum/{controller}/{action}/{Id}",
new { controller = "Forum", action = "Index"}
);
}
Keeps your routes from conflicting, which is what i think is happening in your case. As your default route matches your admin route.

Categories

Resources