Access one web project from another web project asp.net mvc - asp.net-mvc

Problem
In asp.net mvc i have two mvc application in one solution one is website and second is admin panel. And I I have created one folder that name is administrator inside of website project and paste admin project inside of website project now I want access admin section like way :
http://localhost/website/admin
As like nopcommerce but I'm not getting how nopcommerce configured this thing in their project.
Help me out

As others have stated, it may be best to use ares in order to simplify the project, but if you want to keep them separated, I think the problem you are having is updating your routing.
By default, MVC applications have the following routing config (found in the Globals.asax.cs file):
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
What you will need to do is look at the controller you're trying to wire up, and put in a route for that controller. For the sake of an example, I will assume your controller is called "AdminController":
routes.MapRoute(
name: "AdminController",
routeTemplate: "website/admin/{action}",
defaults: new { controller = "Admin", action = "Index"}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
This example shows you how to map the desired route -- http://localhost/website/admin -- to the "Index" action on the "AdminController" object.
For more in-depth ASP.Net routing examples, you can look at the documentation here
Update: After looking at the example library in question (NopCommerce), it would appear they are using an explicit area registration. This is found in 'src/Presentation/Administration/AdminAreaRegistration.cs' :
using System.Web.Mvc;
namespace Nop.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 { controller = "Home", action = "Index", area = "Admin", id = "" },
new[] { "Nop.Admin.Controllers" }
);
}
}
}
Hopefully this gives you a better idea of how this is done.

This tutorial has cleared my confusion that how to configure more then one project in asp.net mvc as area.
http://nileshhirapra.blogspot.no/2012/02/aspnet-mvc-pluggable-application.html?m=1

Related

Routing Issue With Areas

I'm having an issue with routing and areas as the title says. I have ana areas section in my project that just has one area in it. It has a Policy controller in there. Outside in the project there is a controller's folder with a LoginController.
When I try to got to the URL "BoB/Login/ChangeSection" (BoB is project name, Login is controller, ChangeSection is action) it gets sent to "BoB/Policy/Login". This means the Area registration inside the Policy area is grabbing the request and putting the Policy prefix in front when it shouldn't be.
Here is my project RouteConfig
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "BoBHome", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "BoB.Controllers" }
);
and here is the Area registration
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "BoBPolicy_default",
url: "Policy/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "BoB.Areas.BoBPolicy.Controllers" }
);
}
I was under the impression the namespaces would keep this from happening, but it obviously is not. Any help is appreciated.
Some additional info:
This comes from another project that is not MVC so the Redirect looks like this:
Response.Redirect("/BoB/BoBLogin/ChangeSection?controller=PolicyOverview");
I think your issue is similar to non area route issue, which requires usage of area parameter to prevent area URL prefix inserted when using default route:
public override void RegisterArea(AreaRegistrationContext context)
{
// set area parameter with area name
context.MapRoute(
name: "BoBPolicy_default",
url: "Policy/{controller}/{action}/{id}",
// add area parameter here
defaults: new { area = "Policy", controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "BoB.Areas.BoBPolicy.Controllers" }
);
}
NB: Since area route registration always placed on top of default route (read this answer for reasons behind it), it is possible to have routing engine incorrectly picking up default route request into area route when area route constraint(s) not specified.
Related:
MVC5 Routing Go to specific Areas view when root url of site is entered

How do you route from an MVC project to an MVC ApiController in another project?

I've been struggling with this problem for at least 4 hours now, trying every solution I could find.
My main project is set up with Asp.Net and MVC which works great routing to controllers within the project. I'm integrating a library that exposes a JSON api that uses ApiControllers in another project but I can't get the main project to route to anything in the library project, it just returns a strait 404.
In my main project global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{*allaxd}", new { allaxd = #".*\.axd(/.*)?" });
routes.IgnoreRoute("{*less}", new { less = #".*\.less(/.*)?" });
//ignore aspx pages (web forms take care of these)
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.css/{*pathInfo}");
//ControllerBuilder.Current.DefaultNamespaces.Add("Nexus.Controllers");
routes.MapRoute(
"testapi",
"testapi/{controller}/{action}",
new
{
controller = "User",
action = "Get"
},
new[] { "Nexus.Controllers" }
);
routes.MapRoute(
// Route name
"Default",
// URL with parameters
"{controller}/{action}/{id}",
// Parameter defaults
new { controller = "Events", action = "Index", id = "" }
);
}
In my library project UserController:
namespace Nexus.Controllers
{
public class UserController : ApiController
{
public Object Get(string id)
{
var guid = new Guid(id);
var user = crm.Users.Where(x => x.UserId == guid).FirstOrDefault();
return new
{
id = user.UserId,
name = user.UserName,
email = user.Email,
extension = user.Ext,
phone = user.Phone
};
}
}
}
None of the URLs that I expect to work do:
/testapi
/testapi/User
/testapi/User/Get
/testapi/User/Get?id=1
They all return 404.
I've even used the route debugger to verify that the routes appear to be set up correctly:
What am I missing?
You seem to be confusing standard MVC controllers (deriving from the System.Web.Mvc.Controller class) and API controllers (deriving from the System.Web.Http.ApiController class). Your UsersController is an API controller.
So if you want to configure Web API routes you need to use the MapHttpRoute extension method and not the MapRoute which is only for standard MVC controllers.
Like this:
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "testapi",
routeTemplate: "testapi/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Also notice that in a RESTful API you do not specify the action name in the route. It is assumed from the HTTP verb used to consume it. Here's a good article I recommend you reading about RESTful routing in the Web API.

ASP.NET MVC3 Area controller accessible from global routes?

Perhaps I do not understand correctly how MVC Areas work, but this has got me a little confused.
Add an Area called "MyArea" using right-click "Add Area" in Visual Studio on the MVC3 project
Create a controller for MyArea: "AnArea" with matching view in the MyArea area.
Add "controller = "AnArea" to context.MapRoute's defaults parameter in MyAreaAreaRegistration.RegisterArea method.
So at this point if you start the application and navigate to /MyArea/ it should load the AnArea controller with it's matching view. If you navigate to /MyArea/AnArea, it will show the same result.
But, if you navigate to /AnArea/, the controller is still found and the following error message is displayed:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/anarea/Index.aspx
~/Views/anarea/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/anarea/Index.cshtml
~/Views/anarea/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
Is this the correct behaviour? I would have thought an area's controller could only be accessed via it's own area and not globally.
Whenever I create an project with areas, I change my Default route as follows:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // defaults
null, // constraints
new string[] { "MyApplication.Controllers" } // namespaces
);
The final parameter limits the default route to the controllers in the MyApplication.Controllers namespace. This insures that the Default route is limited to actions outside of any areas.
UPDATE
After a deep dive into the code, I discovered where the issue arises, and have a solution. Change your Default route to the following:
routes.Add(
"Default",
new Route("{controller}/{action}/{id}",
new RouteValueDictionary(
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
),
null,
new RouteValueDictionary(
new {
Namespaces = new string[] { "MyApplication.Controllers" },
UseNamespaceFallback = false
}
),
new MvcRouteHandler()
)
);
The key is in adding the UseNamespaceFallback token. This will prevent the Default route from looking into any other namespaces.
This is unexpected behavior, and it was a problem I was unaware of which affects a project I am working on. I will list it as an issue at aspnet.codeplex.com. I would not call this a bug, but the behavior definitely appears to breach the convetions for MVC routing.
You have to apply a namespace restriction in both area and general route.
In global.asax.cs you should edit RegisterRoutes method just like this
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "MyProject.Controllers" }
);
}
That will restrict "//" only to the namespace "MyProject.Controllers"
But also you´ll have to apply the namespace restriction to the Area to restrict "//" only to the namespace "MyProject.Areas.MyArea.Controllers"
For that you´ll have to edit "RegisterArea" method of "MyAreaAreaRegistration.cs" like below ("MyAreaRegistration.cs" is located at "/MyProject/Areas/MyArea" folder ) :
//Some default code stuff
...
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MyArea_default",
"MyArea/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "MyProject.Areas.MyArea.Controllers" }
);
}
Hope it helps!!
You seem to be navigating to /AnArea whereas your area is called MyArea so you should navigate to /MyArea/. Here's how the area route registration looks like:
context.MapRoute(
"MyArea_default",
"MyArea/{controller}/{action}/{id}",
new { controller = "AnArea", action = "Index", id = UrlParameter.Optional }
);
AnArea is the name of the controller, not the area. If you want to navigate to some controller of this area you should always prefix your url with MyArea which is the name of the area.

ASP.NET MVC Area not picking up the correct route

I am in the process of debugging a routing issue on my MVC 3 application and I am using Phil Hacks routing debugger.
I cannot seem to work out where the route highlighted in yellow below is originating. Each time I run my application with the following request
http://www.mywebsite.com/auth/login?ReturnUrl=/
this route comes first and then gives me a 404 error as I do not have an index action. As you can see I have set my default routes to use the Login action method but still this route persists.
I have the following route configurations:
AuthAreaRegistration
public class AuthAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Auth";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"login",
"auth/login/{*returnPath}",
new { controller = "Auth", action = "LogIn", id = UrlParameter.Optional }
);
context.MapRoute(
"Auth_default",
"Auth/{controller}/{action}/{id}",
new { controller = "Auth", action = "LogIn", id = "" }
);
}
}
Global.asax (Using T4 MVC Templates)
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Home",
"{controller}/{action}/{id}",
MVC.Home.Index(), new { id = UrlParameter.Optional },
new string[] { "MyNamespace.WebUI.Controllers" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
MVC.Home.Index(), new { id = UrlParameter.Optional },
new string[] { "MyNamespace.WebUI.Controllers" }
);
}
I don’t like to answer my own question but after a day of trying to solve this problem I thought I would post the answer in case anyone else has the same issue.
It turns out that my application was holding onto old routes and populating them into my route collection. I deleted all the files in my bin folder and rebuilt my solution and everything worked as it should.
I have answered this question in a little more detail here:
Does ASP.NET MVC create default routes for areas
I think the problem is in the fact that you have an area called Auth and a controller called Auth outside of the areas.
MVC will try to match your url against Auth area first but you actually want it to hit your auth controller outside an area.
The best way to solve it imho is to avoid a ambiguous names of controllers/areas.

asp.net mvc area default page

I have a MVC 2 site with an area, let's say the area name is {Admin}
The areas and the site works fine.
What I am trying to do is to have different default page for the area.
When I am calling http://webSiteName works with no problem
but for http://webSiteName/Admin I am getting the error
The resource cannot be found
I tried it out the solutions from ASP.NET MVC 2 RC 2 returns Area-specific controller when no area specified
but with no luck.
I tried also
routes.MapRoute(
"Admin",
"{controller}/{action}/{id}",
new { controller = "AdminHome", action = "index" },
new[] { "Web.Areas.Admin.Controllers" }
);
and
routes.MapRoute(
"Admin",
"Admin",
new { controller = "AdminHome", action = "index" },
new string[] { "Web.Areas.Admin.Controllers" }
);
but I am still getting The resource cannot be found.
What am I doing wrong?
Try this. Make sure it will be in /Areas/Admin/AdminAreaRegistration.cs when your Area is named Admin.
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "AdminHome",action = "Index", id = "" }
);
}
You don't have to add anything to your Global.asax.

Resources