I have an MVC application with a sub application running another MVC project in IIS. Both use the same version framework and run on separate application pools.
My problem is, I cannot get the sub application to work inside this virtual application folder of the root site. I get a 403.14 Forbidden error. If I enable directory listing on the sub application I just get a list of the MVC application files.
I think, I have narrowed the problem down to routing; The sub application has a custom catchall route which handles all requests to the site, its a CMS application. There are no other routes registered. Here is the code for my custom route:
RouteTable.Routes.Insert(0,new CmsRoute(
"{*path}",
new RouteValueDictionary(new
{
controller = "Page",
action = "Index"
}),
new MvcRouteHandler()));
Running this application outside of the main root application in its own site, it works fine and all requests are handled by my custom route. But as soon as I try run this as a sub application the route handler is never hit.
If I add the standard default MVC route that comes out of the box:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Then MVC kicks in and tries to find a match route for the request.
So my question is why will my catchall route not work while running in a sub application in IIS ?
Note: I have tried prefixing the url in my custom route with the name of the virtual application folder, but it still does not work, example:
RouteTable.Routes.Insert(0,new CmsRoute(
"subapplication/{*path}",
new RouteValueDictionary(new
{
controller = "Page",
action = "Index"
}),
new MvcRouteHandler()));
Use sub domain rather than sub application if that doesn't hurt your purpose.
But, if you are bound to do that,remember this the application and sub application have to be running on the same version of .NET.
In addition, there is a parent/child relationship with the web.configs - so the child application will inherit the parents web.config unless told not to do so using 'inheritInChildApplications' in the configs. But, that might also give you headache of clashing with child. here is a solution to this(headache) if you want to try parent/child relationship . http://articles.runtings.co.uk/2010/04/solved-iis7-validateintegratedmodeconfi.html
When you send a request from a client to an application, it doesn't get transferred to another application (like your sub-application) unless you specifically make the call. That's why your sub-application works fine when you access it directly but fails when you try to reach it through the parent the way you're doing it. Routing is about requests and it's the application that got the request that is responsible for the routing issues.
So what you need to do is make an actual redirect to your sub-application. Your sub-application should have a separate URL (a sub-domain will be fine). The code should look like this:
public ActionResult YourAction() //Action in the main application
{
var subAppUrl = "http://yourSubApplicationUrl?AnyOtherQueryStringSetToPassData";
return Redirect(subAppUrl);
}
If you're calling the action using Ajax, then it should be:
public JsonResult YourAction() //Action in the main application
{
var subAppUrl = "http://yourSubApplicationUrl?AnyOtherQueryStringSetToPassData";
return Json(new {url = subAppUrl });
}
and the corresponding JQuery:
$.post("#Url.Action("YourAction")", function(data) {
window.location = data.url;
});
Your SubApplication it's already the Path:
RootApp /
SubApp1 /SubApp1
SubApp2 /SubApp2
routes.MapRoute(
"SubApp1", // Route name
"{*url}", // URL with parameters
new
{
controller = "Controller",
action = "Action",
id = UrlParameter.Optional
}
);
And.. you should remove on the root app the MapRoute to your SubApp1 if already managed!
Related
I have a project that I am upgrading from MVC 2 -> MVC 4. During the transition from MVC 2 -> MVC 3, I noticed that some of my hyperlinks broke and were no longer matching the routes as defined before. I have the following project structure:
...
App_Data
Areas
Test
Controllers
TestController
Views
Models
Controllers
PartialController
Views
Scripts
...
The links that are generated are in the format /Test/Partial/Render. If I go back and run the project before the migration, the PartialController Render action is hit as expected. However, now the app says that it can't find an action because there is no Test/Partial controller. I actually am not sure how it worked before, but can't seem to get it to map correctly.
Is there something I'm missing? Here is the relevant code:
Global.asax.cs:
...
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "" }
);
TestAreaRegistration.cs:
context.MapRoute(
"Test_default",
"Test/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
I did move the Controllers folder up a level to be more in-line with how the project should be structured; beforehand it was in the Areas folder itself. However, none of this worked before I moved that, so I doubt that is the case. I also thought that Autofac had something to do with this, but I am less certain of that because if I shave the "Test" portion out of the URL it matches as expected.
I guess this all boils down to a question on how to check in a "generic" controllers directory first, before matching to an area-specific controller directory, even with an area specified in the URL.
Thanks in advance for your help!
EDIT: If it helps, I noticed that in the existing MVC 2 solution, if I go to Test/Home for example, it will invoke the Index method of the HomeController. However, this does not happen in the new solution. Is this because there is no View associated with the new routes? I have not made new .cshtml files for each of the corresponding .ascx files yet, but I didn't think that should matter much as they are used otherwise.
So apparently the issue was related to namespaces. I had seen that answer on questions like this, but those had to do with collisions finding views. Here is the line I ended up adding to each of my Area registrations:
context.MapRoute(
"Test_default",
"Test/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new string[] { "Project.Web.Controllers", "Project.Web.Areas.Test.Controllers" } //added this line
);
I am fairly new to MVC. Recently I developed a site and hosted it behind a proxy server. I access my site using internalhostname/site/{controller}/{action}/{id} for test, which works fine.
However, when my users are connecting the site, they would use an external url like this: externalhostname/apps/site/{controller}/{action}/{id}. Now when my views attempt to initiate a call to the controller according to the default route config, the URLs being generated become externalhostname/site/{controller}/{action}/{id}, notice "/apps" is gone.
Well, this is a known problem to me. Because when creating the URL, host name does not include "/apps". In other sites created in regular ASP.NET page, I would simply hijack the URL creation and replace host with host/apps, that can fix the issue. But I don't know how to do this in MVC world.
Also I am using Telerik controls (for MVC) that also initiate requests by controller and action, which lead to the wrong URL eventually.
The route config is default. I did try to change the rule but all of those only affect the url format after . Nothing could allow me to change the behavior ahead of it.
I have been struggling for days and couldn't see a way out. Appreciate for some advice. Thank you.
I cannot change the proxy rule. That is not an option to me.
Environment: MVC 4, IIS 7.0, if these matter.
Try to add new entry in your route config.
routes.MapRoute(
"Default2", // Route name
"apps/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults,
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Take note of the order.
I have added the following code
public class RouteController : Controller
{
public ContentResult GetImpression()
{
// Do something
}
}
In RouteConfig class i have added the following
routes.MapRoute(
name: "Impression",
url: "imp",
defaults: new { controller = "Route", action = "GetImpression", id = UrlParameter.Optional }
);
I am expecting my http://mymachine/imp to work. What am i doing wrong? Do i have to do some settings in IIS as well?
For issues with routing I have always found the following tool from Phil Hacck to be invaluable in figuring out where I screwed up with my routing rules and what is getting called. There is nothing special you need to do in IIS to get things working. The one thing you probably should check is to ensure if your Application Pool is using version 4.0 and not version 2.0. Usually when you create a site in IIS it defaults the App Pool to 2.0.
I code lots of ASP.NET but I'm kind of new with .net MVC, I've a default route registered like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And I want to add another Administrator area on the site and all the URL would be something like "http://localhost/Administrator/controller1", "http://localhost/Administrator/controller2", etc. I've lot of controllers in the Administrator namespace and I'm trying to register those controller with only one MapRoute, I did something like this:
routes.MapRoute("Administrator_default", "Administrator/{controller}/{action}/{id}", new { controller = "Administrator", action = "Index", id = "" });
it works with those controller but one problem is that in some other controller while I try to do a redirect like:
return RedirectToAction("Index", "Forum");
Then I'll always be redirect to http://localhost/Administrator/Forum instead of http://localhost/Forum, it's not a big issue but make the URL looks strange, I tried to restrict to certain namespace but it's not working. It looks just as I'm trying to register two default route and .Net just match the first one, I'm wondering is there a way to make it two default route and map on only specific path only?
This exact issue is why Areas were added to MVC 2. http://www.asp.net/whitepapers/what-is-new-in-aspnet-mvc#_TOC3_2
Agree with Zach's answer.
Not ideal, but you do have the option to have controllers in the controller root folder (e.g. /controllers/HomeController.cs) of your project as well as the controllers in Areas (maybe high level root pages that display menus for areas).
Secondly a quick tip on using the RedirectToAction method. You can specify the area you would like to redirect too using the route parameters e.g:
RedirectToAction("Index","Form", new { area = "MyOtherArea" });
I have the following in my Global.asax.cs
routes.MapRoute(
"Arrival",
"{partnerID}",
new { controller = "Search", action = "Index", partnerID="1000" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
My SearchController looks like this
public class SearchController : Controller
{
// Display search results
public ActionResult Index(int partnerID)
{
ViewData["partnerID"] = partnerID;
return View();
}
}
and Index.aspx simply shows ViewData["partnerID"] at the moment.
I have a virtual directory set up in IIS on Windows XP called Test.
If I point my browser at http://localhost/Test/ then I get 1000 displayed as expected. However, if I try http://localhost/Test/1000 I get a page not found error. Any ideas?
Are there any special considerations for running MVC in a virtual directory?
IIS 5.1 interprets your url such that its looking for a folder named 1000 under the folder named Test. Why is that so?
This happens because IIS 6 only
invokes ASP.NET when it sees a
“filename extension” in the URL that’s
mapped to aspnet_isapi.dll (which is a
C/C++ ISAPI filter responsible for
invoking ASP.NET). Since routing is a
.NET IHttpModule called
UrlRoutingModule, it doesn’t get
invoked unless ASP.NET itself gets
invoked, which only happens when
aspnet_isapi.dll gets invoked, which
only happens when there’s a .aspx in
the URL. So, no .aspx, no
UrlRoutingModule, hence the 404.
Easiest solution is:
If you don’t mind having .aspx in your
URLs, just go through your routing
config, adding .aspx before a
forward-slash in each pattern. For
example, use
{controller}.aspx/{action}/{id} or
myapp.aspx/{controller}/{action}/{id}.
Don’t put .aspx inside the
curly-bracket parameter names, or into
the ‘default’ values, because it isn’t
really part of the controller name -
it’s just in the URL to satisfy IIS.
Source: http://blog.codeville.net/2008/07/04/options-for-deploying-aspnet-mvc-to-iis-6/
If you are doing this on Windows XP, then you're using IIS 5.1. You need to get ASP.Net to handle your request. You need to either add an extension to your routes ({controller}.mvc/{action}/{id}) and map that extension to ASP.Net or map all requests to ASP.Net. The http://localhost/Test works because it goes to Default.aspx which is handled specially in MVC projects.
Additionally, you need to specify http://localhost/Test/Search/Index/1000. The controller and action pieces are not optional if you want to specify an ID.
There are a number of considerations when using virtual directories in your application.
One is particular is that most browsers will not submit cookies that came from one virtual directory to another, even if the apps reside on the same server.
Try set virtual path: right click on mvc project, properties, web tab, there enter appropriate location.