Running an MVC Application as a Sub-Application? - c#

I'm attempting to create an MVC application as a sub-application to my standard Asp.Net Web application. Both of these projects are inside the same solution. While the parent application appears to be going fine, I'm having trouble getting the sub-application to work. After some massaging of my two web.configs, I was able to get the Asp.Net runtime to accept the configurations, but I have been unable to browse to any of the pages/controllers in the MVC application, including the root of the sub-application ("http://RootSite/SubApplicationName/"). I continually get 404's.
Actually, I do get a response when going to the url "http://RootSite/SubApplicationName/Home/Index/". It redirects me to index.aspx in that folder, and throws this error:
The view 'Index' or its master could not be found. The following locations
were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
The sub-application in IIS (7) is set up fairly straight forward: it's set to run in the same application pool as the parent app, which runs Asp.Net 2.0 in integration mode.
My suspicion is that I have something in the web.configs that is throwing it off. Are there things regarding, say, HTTPModules or URL authorization modules, etc., that I should confirm aren't getting in the way of MVC?
Also, in the global.asax.cs file, should the default route be different? By default, the url parameter passed to routes.MapRoute is:
"{controller}/{action}/{id}"
Should it be preceded by the name of the sub-application, like so?
"SubApplicationName/{controller}/{action}/{id}"
I attempted a change like that, but it did not fix things.
Any ideas are much appreciated. Also, general information about setting up an MVC web application as a sub-application would be great.
Thanks.

I did something similar, however not the same, I had to load views from a separate dll. In my case it was a class library, not a different web app, but it should work the same as far as I know.
The first thing you have to do is to create a VirtualPath Provider to tell the routing engine how to look for your stuff in the subapplication views. A great explanation of how to do this can be found here:
http://www.wynia.org/wordpress/2008/12/05/aspnet-mvc-plugins/
I'm sure that will get you started ;)

Make sure that you haven't made any spelling mistakes in the names of your Views directories. I was receiving the same error message and after 30 mins of head scratching realized that I had misspelled the folder name for one of my Views. The IDE did not pick this up in any meaningful way (i.e. it would have been nice if it explicitly told me that the path to the view that I was referencing was not correct -- "not found" could mean a few different things).

Sub application doesn't suite to MVC web application directly. you have to write a lot of hacked code in global.asax. Use sub domain rather than sub application.

Related

SSO MVC App compiles without errors but throws 404

I have a very basic Single Sign On app built on VS 2015 using MVC and Web Forms. It is supposed to be a simple proof of concept and is based on some code found here and here which are essentially the same things. I've finally gotten it all converted to use .Net 4.5 but when running it on my local server it throws a 404 with no debug information.
The 404 itself wasn't initially a surprise as I was supposed to be able to change the url to one of the secure pages (for instance /WebSecApp1) which would redirect me back to the signon page but no matter what I put as the url I get the 404.
I've also tried changing the urls in the code so that they contain the port numbers for the localhost but that doesn't work either.
It was suggested to me that the RouteConfig.cs could be the culprit but I don't see how that could be since I'm calling a single page with no parameters.
I know this is kind of lite on details but does anyone have any suggestions?
Yes this looks like a routing issue as you also thought it to be. Routing is essential for web api too .Pls see https://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection. Does your api request look like this
GET http://localhost:34701/api/products/1?version=1.5&details=1
You do have to mention the port in the request.
While the routing that Arathy mentioned above was partially to blame, the real problem turned out to be relatively simple. In my case simply selecting Properties->Web for each of offending pages and setting "Override application root URL" to checked fixed the whole problem.

Separate MVC application as a folder using Handler Mapping

We have a legacy webforms app and a new section of our site developed with MVC. It works a treat as an application in the Default Web Site in IIS.
Only downside is that the root site's session object isn't passed to the application. We have a work around for this.
A consultant is convinced that we don't need to set it up as an application and it can all be done via handlers and route mappings. He conveniently has not provided us with any details though and I have been tasked to implement it.
The best I have achieved was to add a Module Mapping to the Handler Mapping section in IIS. This filters coolmvcapp/* with Module isapimodule to a virtual directory who's physical path points to our new MVC app. The result is an empty page with a 200 response. Most other combinations I have tried resulted in a variety of errors. (Turns out an empty page is returned without adding the handler mapping - hey ho)
So, is this approach even possible?
I am aware that MVC and webforms can be merged together but for reasons this is something that we want to avoid.

Using multiple global error files in the same solution (Web Application)

I have an Web application with 3 sub folders that I want to use in the solution:
HR
Business
Money
Each has a sub application that uses common parts of the solution like Master Page, CSS, etc.
Each application has its own error page in their respective folders.
But there is only 1 Global.asax at the top folder.
I want to handle error at global level for each application so that the unknown errors always land up at the correct error page for each solution when we are browsing their pages.
Eg.
1. Error in /HR/Page1.aspx goes to /HR/ErrorPageForHR.aspx
2. Error in /Business/Page1.aspx goes to /Business/ErrorPageForBusiness.aspx
3. Error in /Money/Page1.aspx goes to /Money/ErrorPageForMoney.aspx
How can I achieve this ?
Can I make multiple Global.asax in each folder or some other way?
A viable approach is to have the logic of your error handler into a Class Library with a Class to handle the error accordingly. This project could also be useful for ther common behaviors shared across the 3 web applications. Then Add that library as reference to each web application, and within each Global.asax file, on the backend, include the Class Library project main namespace and call your ErrorHandler Class logic. That is what I would do...
You cannot have multiple Global.asax, unless you convert folders to Areas which is probebly overkill.
You can get request with HttpContext.Current.Request and redirect user to proper error page based in URL.
Additionally, you can put different web.config file in each folder with customeErrors for 404, 403.

ASP.Net MVC Wrong Application Root Path

I've seen a few questions asked about this, but haven't seen any the same as it (or solved)... So here it goes...
I have two ASP.Net MVC applications, one nested inside the other...
example.com
---sites
------example1.com
Both sites technically work fine, however all URLs for the example1.com domain are resolved to example1.com/sites/example1.com/.
I have the application roots setup correctly in IIS and am using #Html.ActionLink("Home", "Index", "Home") and #Url.Content("~/images/image.png") type references for links...
UPDATE
It looks like the host is showing the HttpRuntime.AppDomainAppVirtualPath is returning the wrong path, /sites/example1.com/ instead of just /.
Any ideas, things to try? Anything would be greatly appreciated.
Turned out to be that the host used some sort of a custom ISAPI extension to route domains to sub directories, and didn't create true application directories. So that explained all of the issues...

How to retrieve on which site the web application is currently running?

I am currently running a WCF service on an AppFabric server and my application needs to load a the web.config file dynamically to retrieve custom configuration sections.
On my development machine I can just load the configuration like this:
WebConfigurationManager.OpenMappedWebConfiguration(webMappedFile, virtualPath);
But on the test machine (AppFabric server) I am getting an exception and it seems that I need to specify a third parameter which is actually the site the web application is running on:
WebConfigurationManager.OpenMappedWebConfiguration(webMappedFile, virtualPath, "MySite");
So I tried to hard code it and it worked. Anyway this is not acceptable, so I need to dynamically provide the site to the WebConfigurationManager because I do not on which site the service will be running in the future. Do anybody knows how to achieve that?
Thanks.
If you are running this code as part of handling a request you could use:
Request.ServerVariables("server_name")
see: http://msdn.microsoft.com/en-us/library/ms525396(VS.90).aspx
Edit based on your comment
The parameter that you need is the Site Name, not the machine name, your code be running on many machines. If the code is running somewhere where it no longer knows that it is on a web site, then it is difficult for it to get the name of the web site that it is running on.
You then have two options:
Send the name as a parameter from a layer that has httpconext
Not sure if this will work: but you could try adding a reference to system.web to your project. It may compile, but you could get a null reference exception when you run it. Probably worth a try.
How about Server.MachineName

Categories

Resources