/Mappings/Index is found but not /Mappings with ASP.NET MVC - asp.net-mvc

Just struggling with a simple issue with ASP.NET MVC. I have a list of views, each view associated with an Index.aspx view being associated by default with /MyView.
Yet, for some reason I have 1 view named /Mappings that does not work (404 resource is not found) whereas the explicit path /Mappings/Index works.
I have the default route settings as provided by the default ASP.NET MVC sample
routes.MapRoute(
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
And, the default Index works for the other views of the same webapp.
Any idea what could be wrong here?

You have to define default action if it is not provided:
route.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { action = "Index" } // Default action if not provided
);
EDIT:
Look at this link:
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
You can use this debugger to test your routing.

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.

Routing to an Area home page in ASP.NET MVC

I'm trying to route to the home page of an Area in MVC, e.g.
myDomain.com/myArea/Home/Index
when I use the URL:
myDomain.com/myArea
MVC appears to by trying to find a Controller called "myArea" in the root Controllers folder and thereby would route to:
myDomain.com/myArea/Index
if the Controller existed.
Again, I want:
myDomain.com/myArea
to route to:
myDomain.com/myArea/Home/Index
I figure that I should be able to do this in Global.asax without having to resort to creating a dummy controller that re-routes to the area controller, but I've drawn a blank.
One way I used to get around this was to set the namespaces property on the default route in the Global RegisterRoutes method, this seemed to resolve that problem. Had to add this restriction to some other routes where I had conflicts between Controller in the main website and the area.
var namespaces = new[] { "CompiledExperience.Website.Web.Controllers" };
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces
);

Default.aspx not getting executed in ASP.NET project with MVC sections

My app is mainly an ASP.NET app that I'm adding an MVC section to it.
My Default.aspx (no codebehind) page has a simple Response.Redirect to a StartPage.aspx page but for some reason MVC is taking over and I'm not getting to the StartPage.aspx page. Instead I get routed over to my first and only MVC section which is a registered route that I've registered in the global.asax.cs page (Albums).
Is there a way to tell MVC to leave my requests to the root "/" to be my IIS 7 default document...in this case Default.aspx?
This is what is in my RegisterRoutes:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Albums","{controller}/{action}/{id}",
new { controller = "Albums", action = "Index", id = "" });
If you remove the default controller from your second route there, it won't match against "/" anymore and Routing will ignore requests for "/", leaving them for the usual ASP.Net pipeline to handle
So, change your routes to:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Albums","{controller}/{action}/{id}",
new { action = "Index", id = "" });
That should solve your problem!
The default.aspx page is being served by IIS because it is the default document. MVC would
let the default.aspx page handle the request, if it realized that the request was for default.aspx (e.g. "http://foo.com/default.aspx"). It doesn't relize that though in this scenario ("http://foo.com") so you could add this before the default route to achieve what you are after
// ignore "/"
routes.IgnoreRoute("");
// default route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
You could tell the MVC to ignore the Default.aspx like this:
routes.IgnoreRoute("Default.aspx");

Asp.net MVC routing ambiguous, two paths for same page

I'm trying out ASP.NET MVC routing and have of course stumbled across a problem. I have a section, /Admin/Pages/, and this is also accessible through /Pages/, which it shouldn't. What could I be missing?
The routing code in global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Pages", // Route name
"Admin/Pages/{action}/{id}", // URL with parameters
// Parameter defaults
new { controller = "Pages", action = "Index", id = "" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
// Parameter defaults
new { controller = "Home", action = "Index", id = "" }
);
}
Thanks!
I'd suggest adding an explicit route for /Pages/ at the beginning.
The problem is that it's being handled by the Default route and deriving:
controller = "Pages"
action = "Index"
id = ""
which are exactly the same as the parameters for your Admin route.
For routing issues like this, you should try out my Route Debugger assembly (use only in testing). It can help figure out these types of issues.
P.S. If you're trying to secure the Pages controller, make sure to use the [Authorize] attribute. Don't just rely on URL authorization.
You could add a constraint to the default rule so that the {Controller} tag cannot be "Pages".
You have in you first route {action} token/parameter which gets in conflict with setting of default action. Try changing parameter name in your route, or remove default action name.

Resources