MVC5 Area not working - c#

I have two Areas in my MVC 5 app that are not working properly.
When I use the following Link http://localhost:45970/Admin/Admin the app loads the proper index.cshtml whicxh is located at /Areas/Admin/Views/Admin/Index.cshtml however when I try to load http://localhost:45970/Admin it tries to load the Index.cshtml file from /Views/Admin/Index.cshtml.
All the search results say I am doing the correct thing. I have even loaded a sample API project to look at the help area in it to make sure I was doing things correctly.
Here is my RouteConfig.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace BlocqueStore_Web
{
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 },
namespaces: new[] { "BlocqueStore_Web.Controllers" }
);
}
}
}
Here is the Application_Start() section of my Global.asax.cs file
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
And finally my AdminAreaRegistration.cs file
using System.Web.Mvc;
namespace BlocqueStore_Web.Areas.Admin
{
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", id = UrlParameter.Optional },
namespaces: new[] { "BlocqueStore_Web.Areas.Admin.Controllers" }
);
}
}
}
So, what am I missing?

You didn't set the default controller when registering Admin area. Set the controller to Admin and action to Index in the defaults parameter of context.MapRoute method
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
defaults: new { action = "Index", controller = "Admin", id = UrlParameter.Optional },
namespaces: new[] { "BlocqueStore_Web.Areas.Admin.Controllers" }
);
}

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

why can I not pass a parameter in the url directly to my method in MVC (the parameter is always null)

So these are the HomeController.cs and RouteConfig.cs. When I tried to write the URL : localhost/Home/Index/Sometitle, the parameter is always null. The same thing happen when I wrote the URL : localhost/Home/Ajouter/SomeTitle. I already tried to find on the internet an answer but I had no success. Can someone tell me what's wrong or missing?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wiki.Models.DAL;
namespace Wiki.Controllers
{
public class HomeController : BaseController //Controller
{
Articles allArticles = new Articles();
// GET: Home
[HttpGet]
public ActionResult Index(string title)
{
if (String.IsNullOrEmpty(title))
return View();
else
return RedirectToAction("Index", "Article", new { title = title });
}
[HttpGet]
public ActionResult Ajouter(string title)
{
if (ModelState.IsValid)
return RedirectToAction("Edit", "Article", new { title = title });
else
return View("Index");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Wiki
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Wiki",
// url: "Wiki/{titre}/{action}",
// defaults: new { controller = "Wiki", action = "Index", titre = UrlParameter.Optional }
//);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
You need to change your parameter name to Id:
public ActionResult Index(string Id)
or replace the route with this:
routes.MapRoute(
name: "CustomName",
url: "{controller}/{action}/{title}",
defaults: new { controller = "Home", action = "Index", title = UrlParameter.Optional }
);
What I really recommand is to read more about routing in MVC
Your route must match the controller variable name try this.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{title}",
defaults: new { controller = "Home", action = "Index", title = UrlParameter.Optional }
);

Routing doesn't find controller with the same name as area

I followed article about Areas in ASP:NET MVC 4 (section How to Avoid Name Conflict). I'm using MVC 5, but I suppose all the features from version 4 are available and should work in version 5.
My directory structure:
File EpicAreaRegistration.cs content:
namespace App1.Web.UI.Areas.Epic
{
public class EpicAreaRegistration : AreaRegistration
{
public override string AreaName
{
get{ return "Epic"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRouteLocalized(
name: "Epic_default",
url: "Epic/{controller}/{action}/{id}",
defaults: new { controller = "Pm", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "App1.Web.UI.Areas.Epic.Controllers" }
);
}
}
}
My project's App_Start -> RouteConfig.cs file content: UPDATE corrected namespace
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 = "Default", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "App1.Web.UI.Controllers" } // according to article namespace must be added here, so ASP.NET router distinguishes between request: e
);
}
}
And finally I have EpicController.cs file in project's directory Controllers:
namespace App1.Web.UI.Controllers
{
public class EpicController : Controller
{
public ActionResult Browse()
{
return View();
}
}
}
When I navigate to: http://localhost:7300/Epic/Pm it works (finds it), but http://localhost:7300/Epic/Browse doesn't work (404 - not found). What have I missed?
My assumption is that request goes through some kind of routing table. If it doesn't find Epic/Browse in Areas, it should move to project's root Controller folder. It's the same analogy as with Views (folder, if not in folder look in Shared, ...)
Additionally I registered all areas in Application_Start
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
....
Make sure your Area controller uses the correct namespace.
Namespace for EpicController is currently:
namespace App1.Web.UI.Controllers
Change it to:
namespace App1.Web.UI.Areas.Epic.Controllers
Your controller is also Epic. So your URL would look like
http://localhost:7300/Epic/Epic/Browse
This may help you to access,
http://localhost:7300/Areas/Epic/Epic/Browse
As It's contained in sub folder.

Route Mapping not working with "area"

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!

Not able to route to a view under area in MVC Razor

Problem Statement:
I'm trying to route to a Login view under Area(Test Area) not working.
Exception:
HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory
Most likely causes:
A default document is not configured for the requested URL, and directory browsing is not enabled on the server.
If I route to view other than the login view under area it works fine
What I'm doing wrong in routing??
Area:
Test Area Registartion.cs
public class TestAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Test";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Test_default",
"Test/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
Roue Config in App_Start :
If I use default route for login it works fine ,but if I give route to login view under test area it gives HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this director why??
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
// Default Route for Login
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }
);
//Area View Route for Login
routes.MapRoute(
name: "Test",
url: "Test/{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "WebApplication1.Areas.Test.Controllers" }
);
}
}
Global.asax.cs :
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
Database.SetInitializer<WebApplication1.Areas.Test.Models.Test_DB>(null);
}
Try with this:
routes.MapRoute(
name: "Test",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Login", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "WebApplication1.Areas.Test.Controllers" }).DataTokens["area"] = "Test";

Categories

Resources