ASP.NET MVC Routing not Working in Virtual Directory - asp.net

I have an asp.net mvc 2 app (using .net 4.0) that isn't routing correctly when hosted in a virtual directory. I have the following simple routing rule:
routes.MapRoute(
"Default", // Route name
"{action}", // URL with parameters
new { controller = "accounts" } // Parameter defaults
);
I'm trying to resolve http://mydomain.com/accounts/new. Where "accounts" is a virtual directory. If I put the app in the root of an IIS website it routes fine for http://mydomain.com/new, but if I put the app in a virtual directory I get 404 errors. I've debugged and it is executing global.asax and configuring routing when in the vdir. Is there something special I need to do for routing in a virtual directory?
FYI. I'm using a vdir because the root has wordpress in it.
Thanks!
one more thing is that if I specify a default action in parameter defaults, it will execute the default action/controller, but it never matches anything else.

I figured it out. Wordpress (which I had installed at the website root) had configured some URL rewrite rules which were preventing asp.net mvc receiving any requests other than the virtual directory root. Anything with a path beyond that was being rewritten to index.php which of course didn't exist in my mvc app.
I removed the rewrite rule, and now everything works as expected.

does it work if you change it to:
routes.MapRoute(
"Default", // Route name
"accounts/{action}", // URL with parameters (BUT WITH ACCOUNTS PREFIX)
new { controller = "accounts" } // Parameter defaults
);

Related

Problems deploying a mixed asp.net-core/ SPA application to IIS

Deploying my web app, I can't get my apis working. Everything works fine in development on my machine.
I have an SPA front and a asp.net back. I hit publish and I get the following folder structure.
publish
|-MyProj.dll
|-MyProj.exe
|-web.config
|-wwwroot
|-assets
|-all my spa.js
|-index.html
I deploy all of that into the root of a new IIS application underneath Default Web Site.
When I browse to http://my-server/MyAppName/, my spa index.html is served, and that side is all fine. However, when the spa starts emitting api requests to http://my-server/MyAppName/api/some-route, the route is not resolved and IIS defaults to returning my index.html.
In my controllers, I use attribute routing... [Route("api/some-route")]. Just to test, I tried modifying a route attribute to [Route("MyAppName/api/some-route")] but I still get index.html. I know my Startup.cs is being processed because if I rename index.html in the code below, everything breaks and I don't see my spa anymore...
// Startup.cs
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "api/{controller}/{action=Index}/{id?}");
endpoints.MapFallbackToFile("index.html");
});
What do I need to configure to tell my app that it's in a virtual dir, and that requests to virtual-dir/api are to be served by asp ??????

Routing Template results in a 404 when navigating to English /en/ site

I have one basic routing template on my newly created project (.NET Core 2.2) (below).
routes.MapRoute(
name: "default",
template: "{parameter}/{controller}/{action}/{id?}"
);
If I try to go to localhost:5000/en/default/index it is not working (I get a 404). Something except "en" is working properly like localhost:5000/abc/default/index or localhost:5000/xyz/default/index.
Why is this happening? How can I fix it?
When I try to run my web app on IIS Express, it automatically adds configuration to applicationhost.config which is located at {SolutionFolder}\\.vs\\{SolutionName}\\config\\applicationhost.config. And it has virtual directories starting with "/en/". I don't know why IIS Express adds this virtual directories but that was the actual redirection problem.

Cannot find Account/Authorize when MVC Web App

I have an MVC app having the following default routing map:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Startup", action = "Index", id = UrlParameter.Optional }
);
As long as I deploy this site to the root of my IIS server (c:\inetpub\wwwroot) and create a new website within IIS, everything works just fine.
However if I create a directory within the wwwroot folder structure, deploy the same files there and instead of creating a website, I create an Application underneath the DefaultWebSite and point it to the subfolder where the files reside (c:\inetpub\wwwroot\MyApplication); when I launch the WebApplication that previous was configured as a website, I get the error "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable."
Requested URL: http://localhost:80/Account/Authorize?client_id=web&response_type=token&state=
Physical Path: C:\inetpub\wwwroot\Account\Authorize
So it would appear that my routing needs to be adjusted to reflect the location of the app is in a subdirectory under wwwroot and not in wwwroot where it is looking.
What provisions should be taken to ensure an MVC Web "Application" runs properly under a Parent website?
=================================
UPDATED OBSERVATIONS
After considering a posted comment, I found a few more details of my issue may be noteworthy to mention.
My application is redirecting to the requested URL I posted by default, I am not doing it. In other words, if I open IIS, right-click my application under the DefaultWebSite and select browse; I am getting the incorrect URL. I don't ever recall having this issue with ASP.NET applications which suggests to me "routing" problem
If I alter the URL so that the Application Name precedes the Account/Authorize path I get redirected again to http://localhost/#access_token=&token_type=bearer&expires_in=1209600 and receive a blank content page that says "Beginning Execution" in the upper right hand corner. The title of the page says CORS Detection Script. Suggesting to me that my application possibly resides in a different domain that the default website??? Need some clarity on this though.
If I again precede the URL that landed me on the CORS Detection Script page with the Application Name, my page successfully loads as I'd expect
================================================
So again, I believe there are some adjustments required somewhere within my app (probably within the routing configuration) to compensate for the incorrect URL being generated by default.
Because if I setup the application as a website with all content at the root instead of a subdirectory, everything works.
Surely someone has seen this before
When you're creating an application in a subfolder of your website, you're effectively only changing the scope of the process running your app (w3p.exe). You will still need to put the application name in the url, like this:
http://localhost:80/MyApplication/Account/Authorize?etc

Web forms routing and keywords in route url

I am having routing weirdness. Are certain keywords a problem in route paths. For example, this route works:
routes.MapPageRoute("AdminLogin", "horse", "~/AdminAccount/Login.aspx");
But this route to the same resource fails:
routes.MapPageRoute("AdminLogin", "admin", "~/AdminAccount/Login.aspx");
It does a login redirect to the root loginform designated in the web.config file and the whole site locks up.
Found it. There was a folder in the project directory called Admin. It was defaulting to that folder, which is behind security.

MVC 3 Deployment to IIS6

Originally, I deployed the site with some of the DLL's set to copy local (as haacked suggested on another SO post). After this failed, I installed MVC 3 package on the server.
When I hit my site I get:
Directory Listing Denied This Virtual
Directory does not allow contents to
be listed.
I saw haacked's blog, but its almost 3 years old now and some of it just doesn't work anymore. Does anyone know what we need to do to get this to work after we installed MVC 3 on the server?
UPDATE
I found another link on haacked's blog. Apparently, this should just work (no need to do wildcard mappings). My website virtual directory is set to run ASP.NET 4.0.30319, I have installed ASP.NET MVC 3, and I am running IIS6 (windows server 2003 R2, Pretty sure that is IIS6).
The Virtual Directory was set to ASP.NET 4.0. While, the "Default Web Site" was set to run ASP.NET 2.0. I changed the "Default Web Site" to run 4.0 in addition to the virt. directory and it started working. The reason is a mystery to me, but it worked.
It sounds like you haven't set up the wildcard mapping. You need to configure a mapping in IIS that sends all requests to ASP.NET pipeline for processing. Since you don't have the mapping set up, it interprets the request as a directory browse (which is probably disallowed in your configuration).
From Haacked:
In the IIS properties for the website, click the Home Directory tab.
Click the "Configuration..." button. In the "Mappings" tab, click "Insert..."
Next to the "Wildcard application maps" label In the textbox, type in "c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll"
Uncheck the box labeled "Verify that file exists" Click OK
I believe ASP.NET 4 Extensionless URL feature is not working for virtual directory on iis v6 unless Default Web Site is also set to run ASP.NET v4.0.
For me, setting ASP.NET v4.0 to Default Web Site is not an option since it's breaking existing v2.0 virtual directory webapps.
Adding wildcard application maps on mvc3 virtual directory webapp worked for me.
Here's the list of steps that I did to deploy mvc onto iis6.
ASP.NET v4 is already installed on Windows 2003 server
Install AspNetMVC3Setup.exe
Copy the mvc3 webapp files onto server without mvc3 dlls in the bin folder
Create Virtual Directory pointing to the app folder
Set the ASP.NET version to 4.0.30319
Add Wildcard application map to c:\windows\microsoft.net\framework\v4.0.30319\aspnet_isapi.dll and uncheck the Verify that file exists
ABOUT WILCARD APPLICATION MAP: Be aware that this option causes IIS to intercept every request made against the web server. This includes requests for images, classic ASP pages, and HTML pages. Therefore, enabling a wildcard script map to ASP.NET does have performance implications.
OTHER SOLUTION: In W2K3 SP2 with IIS6, .NET Framework (2, 3.5, 4).
Goto Registry:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET\4.0.30319.0
If is necesary create DWORD EnableExtensionlessUrls with value 0.
Then from command window execute: resetiis
Create a ASP.NET application from MVC3 Template.
Now in the global.asax setup the routes like theses:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// "Default", // Route name
// "{controller}.mvc/{action}/{id}", // URL with parameters
// new { controller = "Home", action = "Index", id = "" } // Parameter defaults
//);
routes.MapRoute(
"Default",
"{controller}.aspx/{action}/{id}",
new { action = "Index", id = "" }
);
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "About", id = "" }
);
}
Create AppPool for .NET Framework 4 and setup the web application to use this apppool. You can use the Denis Bauer's ASP.NET Version Switcher or use Aspnet_regiis.exe.
RESULT: the browser shows de About page as the default page.

Resources