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");
Related
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.
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
I have a MVC 3 Razor app and I need to deploy it on http://www.mydomain.com.
The trouble is that that, when I hit http://www.mydomain.com it gives a 404 Error. I need http://www.mydomain.com/Home view loaded by default, I would like to avoid using a redirect method as it is not SEO friendly...
If you haven't chanegd the RegisterRoutes in the Global.asax.cs file, then it defaults to /Home/Index
see below.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
So the 4th line of code is where the defaults are configured.
As for the default setting, you can use the RegisterRoutes(RouteCollection routes) method that is available in Global.asax.cs to mark the default controller and its corresponding action method. Regarding hitting www.mydomain.com and getting a 404 error, I need more information. if you are on windows and working on a developer machine, did you add the entry to hosts file located in
C:\Windows\System32\drivers\etc
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.
I need the correct Global.asax settings in order for my Dynamic Data site to run under an ASP.NET MVC project. The routing currently appears to be my issue.
Here is my global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
MetaModel model = new MetaModel();
model.RegisterContext(typeof(Models.DBDataContext), new ContextConfiguration() { ScaffoldAllTables = true });
routes.Add(new DynamicDataRoute("DD/{table}/{action}.aspx") {
Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
Model = model
});
routes.MapRoute(
"Assignment",
"Assignment/{action}/{page}",
new { controller = "Assignment", action = "Index", page = "" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Admin", action = "Index", id = "" }); // Parameter defaults
}
Link that I'm trying to use is:
http://localhost:64205/DD/Work_Phases/ListDetails.aspx
I am getting the following message:
Server Error in '/' Application. The
resource cannot be found. Description:
HTTP 404. The resource you are looking
for (or one of its dependencies) could
have been removed, had its name
changed, or is temporarily
unavailable. Please review the
following URL and make sure that it is
spelled correctly.
Requested URL:
/DD/Work_Phases/ListDetails.aspx
I've tried replacing DD with DynamicData since the folder inside of the app is DynamicData and that yielded the exact same result.
The URL
http://localhost:64205/DD/Work_Phases/ListDetails.aspx
is matching your second (default) route, which is trying to hit a controller called "DD".
You may need another route entry that looks something like this:
routes.MapRoute(
"DD",
"DD/{action}/{page}",
new { controller = "NameOfController", action = "Index", page = "" }
);
...although I can't imagine why you would need to pass a page parameter. The page view that is hit depends on the return action of the controller method.
For a better look at integrating Dynamic Data with ASP.NET MVC, have a look at Scott Hanselman's Plugin-Hybrids article. He has some details about handling the .ASPX files that are not part of MVC. In particular, if you have an .ASPX that you don't want to be processed by the ASP.NET MVC controllers, you can install an Ignore Route:
routes.IgnoreRoute("{myWebForms}.aspx/{*pathInfo}");
It should be noted that ASP.NET MVC is configured out of the box to ignore URL requests for files that physically exist on the disk, although Scott's IgnoreRoute technique is apparently more efficient.
The url doesn't match your dynamic data route because it doesn't fit the constraints you put on it. You're requesting action ListDetails but only these actions are allowed
Constraints = new RouteValueDictionary(
new { action = "List|Details|Edit|Insert" }
EDIT: are you sure that an action called ListDetails exists? Then modify the constraints above to
Constraints = new RouteValueDictionary(
new { action = "ListDetails|List|Details|Edit|Insert" }
Just to be sure that it's the constraints that's causing the route to be ignored, can you try one of the default actions? E.g.
http://localhost:64205/DD/Work_Phases/List.aspx
For ASP.NET MVC to work, you will have to match the URL you are trying to access with the list of routes.
For your current global.asax, example of valid URLs are:
http://domain/AnyController/AnyAction/AnyParameter
http://domain/Assignment/
http://domain/Assignment/AnyAction/AnyParameter
MVC requests are redirected to the proper Controller class, Action method, with parameters as passed in. MVC request is not redirected to any ASPX class. This is the difference between ASP.NET MVC and vanilla ASP.NET Page.