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.
Related
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" }
);
}
}
}
MVC5 project I created the area for the admin panel.
I got a problem.
There are ProductController and ListActionResult on the front of the site.
And.
There are ProductController and ListActionResult in the admin panel.
Now...
I am going to admin panel ProductController and ListActionResult.(www.xxxxxxxxx.com/Admin/Home/List)
But the project open on its front page. (www.xxxxxxxxx.com/Home/List)
Page I want to open >> www.xxxxxxxxx.com/Admin/Home/List
But this opening page >> www.xxxxxxxxx.com/Home/List
It does not change the page address.
But it going controller.
What could be the problem.
Below are the route information.
AdminAreaRegistration.cs;
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context )
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", controller = "Home", id = UrlParameter.Optional },
new[] { "Projem.Web.Areas.Admin.Controllers" }
;
}
RouteConfig.cs;
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 },
namespaces: new[] { "Projem.Web.Controllers" }
;
}
}
change in RegisterArea method like this.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", area = "Admin", id = "" },
new[] { "Projem.Web.Areas.Admin.Controllers" }
);
}
[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 }
);
}
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
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!