View in Controllers served when attempting to access View in Area - asp.net-mvc

I have encountered an issue in ASP MVC whereby I am attempting to access a View in the admin Area App/Areas/Admin/Account/Manage, however the page sometimes serves the user profile page in the root namespace controller App/Account/Manage.
It seems to be after I make a change to the admin View, but then works normally again after the debugger is restarted.
I am using RazorGenerator, so I suspect it's something to do with that, however I don't want to run the risk of another developer publishing the app without updating razor generator, and an admin having access to the users profile page.
All routes are being registered with AreaRegistration.RegisterAllAreas(); in the Global.asax file
The default route config includes the app namespace;
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "App.Controllers" }
);
And the admin area registration contains the routing;
context.MapRoute(
name: "Admin_default",
url: "Admin/{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "App.Areas.Admin.Controllers" }
);
I have setup a break point, and can follow the code through the Admin controller, however as soon as the return View(model) line is hit, it attempts to redirect to the user profile rather than the admin view.
Has anyone encountered something similar?

Related

Default route in MVC project with areas

I have an ASP MVC 4 project which contains areas and is structured as follows:
Areas
MyArea
Controllers
MyAreaController.cs
MyAreaRegistration.cs
Controllers
HomeController.cs
Global.asax
In Global.asax, I register first the area routes and then the global routes:
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
I have a default route in RegisterRoutes:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "Controllers" }
);
Now, I want the default route of the project to point to an action (the login page with an return url as parameter) in MyArea without affecting the rest of the routes. I tried to set a default route in MyAreaRegistration:
context.MapRoute(
"MyArea_Default",
"{controller}/{action}/{returnUrl}",
new { controller = "MyArea", action = "LogOn", returnUrl = "/Home/Index" },
new[] { "Areas.MyArea.Controllers" }
);
This is working, the projects starts on the desired view. However, the problem is that many routes used across the project (for example /Home/Help) will match the MyArea_Default route since it is registered before the Default route. Trying to set the area DataToken in RegisterRoutes also messes up every other route not in the area.
How can I achieve a default route in the area without messing up everything else?
Any help is appreciated.

Angular 2 - Routing with ASP.NET MVC

I am trying to use ASP.NET MVC (not core) with AngularJS 2 and having some issues with the routing.
First in RouteConfig.cs, I have following routes defined
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// when the user types in a link handled by client side routing to the address bar
// or refreshes the page, that triggers the server routing. The server should pass
// that onto the client, so Angular can handle the route
routes.MapRoute(
name: "spa-fallback",
url: "{*url}",
defaults: new { controller = "Home", action = "Index" }
);
In my app.route.ts (angular routes), I have just defined a couple of routes. My default route redirects to the other route like
export const router: Routes = [{
path: '',
redirectTo: 'auctions/e231',
pathMatch: 'full'
},
{
path: 'auctions/:id',
component: AuctionComponent,
children: []
}
];
When I run the application, my server route /Home/Index is served up fine which loads the angular application and default route in my app.route.ts redirects me to auctions/e231 and my browser's final URL becomes
http://localhost:53796/auctions/e231
Everything works as expected but when I refresh the page with this URL, I get a server error for resource not found, which is also expected because it looks for a Controller named Auctions which is not present in MVC. I want to know why my spa-fallback route in RouteConfig.cs doesn't picked up? Also is there a better way to handle this scenario in asp.net mvc because I want to able to use some of my MVC controller's actions like /Account/Login and others.
The problem is that when you refresh the page, the first route will be used, because it will try to get a controller with name Auctions. If you remove that first route configuration (Default) and only use the second (spa-fallback), it will work ok, that's how I used in my projects, only put before spa-fallback other mvc routes that you want to redirect to other mvc controllers.
Although not the best approach in my opinion but worked nevertheless. I made it work using the constraints in my route. I updated my default route to:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { controller = "Home|Account|Upload|Resource" } // this is basically a regular expression
);
Where I only had 4 MVC controllers (Home, Account, Upload, Resource). My spa-fallback route is under the default one so if we type anything other than these controller names, angular will try to find it in its route with the default /Home/Index server path.
I hope it helps someone.

Can I have a default.aspx page in an MVC site?

I've got a legacy WebForms site with a Default.aspx page that's configured in IIS to be the default page. So when someone goes to mysite.com they see mysite.com/Default.aspx, but the url in the address bar only shows the mysite.com.
I added MVC to the site, and I want to gradually move functionality to MVC. Everything works, but it broke the root page: navigating to the route caused a 404 because the default route was matching the root and trying to route a the Home controller, but there wasn't one. So I added one that does this:
' GET: /Home
Function Index() As ActionResult
'jump to the go page
Return Redirect("/default.aspx")
End Function
This now works, except that when you navigate to the route, it shows mysite.com/Default.aspx in the address bar.
I'd like it if either:
I could NOT match the root route, and let the WebForms Default.aspx page handle it as before
I could route as I'm doing now, but make it so the Default.aspx page was not displayed in the address bar.
Are either of these possible?
It is possible.
In your route configuration, remove the controller in the defaults parameter.
From this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
To this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
Then add the Default.aspx to your project root

Why is my area routing failing for one controller but not another?

I have an MVC 4 site with the usual default routes defined (including an API route), plus an area for administration functions, which has its own route. The routing configuration looks like this:
Default Route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyProject.Controllers" }
);
Default API Route:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Admin Area Route (in the area registration code):
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyProject.Areas.Admin.Controllers" }
);
Additionally, the admin area has it's own Layout page for the views.
The routes for the main site all work correctly (including the HTTP routes for the API controllers), but the routes for the Admin area show some odd behaviour. Requests made to the home controller in the admin area succeed, whilst requests to other controllers in the admin area do not. The error that I get is
System.Web.HttpException: The controller for path '/admin/concerts' was not found or does not implement IController.
The interesting thing is that the stack trace associated with the exception contains code from the main Layout page, rather than the admin area's Layout page, which I think suggests that the request has been routed to the default route rather than the admin route.
I've tried debugging the route configuration with Glimpse, but haven't had much luck, other than to confirm through a second medium that the route works correctly for the admin area's HomeController, but not for other controllers in the area.
Update:
I have the following relevant controllers defined:
Default Route:
MyProject.Controllers.ConcertsController
MyProject.Controllers.HomeController
(Some others, not relevant here.)
Default HTTP Route:
MyProject.Controllers.Api.ConcertsController only
Admin Area Route:
MyProject.Areas.Admin.Controllers.ConcertsController; and
MyProject.Areas.Admin.Controllers.HomeController only
This turned out not to be a routing issue at all, but instead a problem with the view definitions: each of the ConcertsController views included a Layout statement pointing the view at the Layout for the main site.

Deployed the mvc 4 site on hosting server but the all the links of the site landing to logon page instead of their respective pages

Deployed the mvc 4 site on hosting server but the all the links of the site landing to logon page instead of their respective pages. I tested my Routes even and the application is running fine on my local.
Here are my Routes
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "logon", action = "Index", id = UrlParameter.Optional }
);
Please give me solution for this.
Check your web.config for forms authentication settings, and do you have the [Authorize] attribute on your controller class? It sounds like since the user is not signed in, he is always getting redirected to the login page.

Resources