How to map sub site requests to the parent site? - c#

We've got one monolithic ASP.NET application at, say, https://my-site.com.
Inside it there is a number of controllers at path /api, say,
https://my-site.com/api/user
https://my-site.com/api/order
https://my-site.com/api/cart
Now we're trying to split the monolith into separate services. These services are separate .NET Core applications. And they are going to gradually replace all those controllers in the /api path. So while some controllers in the monolith should still be working, we need to add separate applications to handle requests for, say, https://my-site.com/api/user.
The site is hosted in IIS. If we create a sub application api for the https://my-site.com site , IIS starts mapping all requests to /api/whatever to the api sub application and not to the parent site. How can we make IIS map requests only for paths really present in the api sub application while mapping others to the parent monolith?
What we've tried so far...In web.config of the monolith in system.webServer we added an entry to the handlers section like this:
<add name="api-order" verb="*" path="api/order*" type="Monolith.Controllers.Api.Order" preCondition="managedHandler"/>
With this we wanted to make IIS map /api/order requests to the monolith and thus ignore the existing api sub application.
However it turns out that IIS chooses the sub application at first and checks its web.config already after that.
So is there any way to cheat IIS and make it map only certain requests to the api sub application?

Related

ASP.net mvc routing with multiple sites under the same route url

Good morning,
I have a fairly large MVC project with 30/40 controller and a lot of views, i am trying to publish on IIS using multiple application so i can split various sites per different customer.
I want to use my landing page as the base url:
https://somewebsite
I then want to host multiple applications (e.g the same project with different config) as separate applications but under the same main host name.
https://somewebsite/Customer1
https://somewebsite/Customer2
Now i can get my multiple websites fine under the seperate host as application, but when i add my landing page (as the full application) its corrupting the other two sites.
My thoughts are that the routing in the route app (the one i want just for landing page is causing the issue).
Any ideas?
If you are deploying via WebDeploy it will delete subfolders under the host site if they are not part of the host application. You can disable this behavior however with a MSBuild argument:
/p:SkipExtraFilesOnServer=true
OR during the MSDeploy deployment using the following argument:
-enableRule:DoNotDelete
http://www.dotnetcatch.com/2016/02/01/webdeploymsdeploy-quick-tip-keep-existing-files-during-deployment/
The issue lay within the web.config of the applications. The config was being inherited from the master site and causing issues across the board.
Set the clear attributes in the child application config and all is well with the applications.

Dynamically Set Web.Config at Application Start for MVC

I have a web application built in ASP.NET MVC that will be used by several clients. Each client has its own database to store information, but each instance will have the same functionality. I am using tips from this question/answer How can I host multiple websites in IIS 7 and use the same MVC application for all them? to consolidate to a single app folder for easier maintenance.
Right now, I setup each client in its own folder and each with its own Web.Config file. There are a couple of appConfig settings as well as the connectionStrings that are unique to that client.
What I would like to do is have a single app folder that contains the MVC project, but the application then dynamically pulls the correct web.config folder (stored in another folder.) I will select the web.config probably based on the host settings (i.e. www.domain.com loads the domain.web.config file.)
I would like for this to update the behavior of the ConfigurationManagement object. I have seen several posts on how to load a configuration file (and most are regarding app.config for desktop apps) but not how to make it a semi-global change or unique characteristics of ASP.NET MVC and web.config.
How can I accomplish this?
What you are looking to do is called multi-tenancy. And this is quite complex task.
You can't really play about with web.config substitution and for one request give one file and for another request give another config. What if 2 requests from different tenants will come exactly at the same time?
At the moment we are converting our application to multi-tenancy and this is very complex task, so can't really be described in one answer on SO.
You can have a look on this write-up http://www.scribd.com/doc/140181293/setting-up-an-mvc4-multi-tenant-site-v1-1
Also if you google for "multi-tenancy MVC" you'll get many articles on that.
Basic principles for our multi-tenancy look like this: DI container is aware of different tenants and knows that request for tenant1.site.com should use configuration set 1 and for request tenant2.site.com configuration set 2 should be used.
Apart from DI container, no other component knows about tenants. And DI container orchestrates the configurations. Connection strings are sitting in configuration objects and these objects are given to EF contexts before they are created... somewhat complex.
If your case is simple and you don't use caches, to substitute the connection strings, I'd save them outwith web.config and provide them based on tenant request. Probably you can get away without complex DI setups.
The overhead of maintaining multiple config files is small if you use external config files to isolate the connection strings that are unique to the customer leaving the bulk of the config file unchanged.
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
For more options about resolving config information at runtime look at
http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx
For app settings in an internal file see Moving Settings to another config file

IIS7 / MVC4 Routing: Run different routes in separate app pools?

Can I configure IIS7 to run the actions of two separate controllers under two separate routes in different app pools?
Using old .svc web services, I could separate service calls by having a separate .svc file under a particular directory, whereby I could make the directory a Virtual Directory allowing me to run the service as a different identity (or with different authentication settings). This gave me the ability to have a single code base deployed to separate web structure locations.
I now have a single MVC (/WebApi) application, with multiple service APIs. Some of these service calls have to be run in a different app pool from the rest for security purposes.
Despite being separate service methods on separate routes within the application, IIS only sees it as a single application, and (apparently) has no way to map the virtual routes within MVC to Virtual Directory settings.
Creating a separate MVC application and hosting it in a Virtual Directory under the root site causes issues around web.config inheritance, which again I haven't found a way around.
Having said all this, my knowledge of IIS7 is very limited, and googling just brings up posts based on keywors that don't cover this particular scenario.
Would appreciate any help.
Regards,
Rob.
Web.config is inherited in directories and virtual directories, but not across applications. Please make sure your subdirectory is set up as an application (right click on parent, select add application or right click on sub directory and select "convert to application"). At this point each application has it's own web.config file.

ASP.net MVC 4 web application with web service sharing its code

I have written a MVC web application that is used on our corporate intranet. We also have an MVC web site that is a seperate web application that is used by our customers. For our website to talk to our intranet systems database I have written a c# web service to access it.
The problem I have now is in the intranet application I have a complex routine that does some calculations that I now need in the web service for the website to do the same thing. I do not want to copy the logic and have it repeated in 2 locations for obvious reasons, so I need the web service and web application to share the same code base. I know this is possible but what is the best was to go about the use of application variables stored in web config files. For example if I move the code out of the intranet app and into a class library I will loose the functionality of being able to access the webconfig file, so where do I put these variables?
Any Suggestions on the best way to do this?
I will loose the functionality of being able to access the webconfig
file, so where do I put these variables?
You won't loose that ability. Every part of your application can access the configuration file, for instance by using the ConfigurationManager.AppSettings property.
Put the shared code in a class library that is referenced from both the web site and the web service.

Is there a port of the Mixed Authentication Disposition ASP.NET Module (MADAM) for ASP.NET MVC?

The Mixed Authentication Disposition ASP.NET Module (MADAM) is exactly what I need for the project I'm building in MVC2. I'm not an expert on authentication, could MADAM be quickly retrofitted to work with the MVC pipeline?
http://msdn.microsoft.com/en-us/library/aa479391.aspx
Depending on your Server Version of IIS, you will need to place the MADAM modules into different locations within your web.config file.
IIS 6
<system.web>
<httpHandelers>
<-- Madam Modules go here -->
</httpHandelers>
</system.web>
IIS 7
<system.webServer>
<httpHandelers>
<-- Madam Modules go here -->
</httpHandelers>
</system.webServer>
I have been looking into using MADAM for my current project at work as well, however I cannot seem to get it to fire.
It would appear that something has changed in the way that either ASP.Net processes requests and deals with HttpModules or it is a difference in the ASP.Net MVC pipeline.
The quickest solution that I have been able to find is to split the project into two separate projects and host them as different applications in IIS otherwise the authentication will not work as desire.
As far as I can tell if you leave Forms Authentication on, MADAM does not fire and any paths which you wanted Http-Basic authentication for just redirect to the login/or default page. If you have Forms Authentication off then the Http-Basic authentication will work, but the Forms Authentication will not work as it cannot read the .ASPXAUTH cookie automatically.
Unless I stumble across some way to get it working I will have to split this project into two and host each as a separate application in IIS.
If you have any other work arounds they would be gratefully received.
I haven't used MADAM, but based on the diagram I don't see any reason why you couldn't implement this process in an AuthorizationFilter or wire it up directly to HttpApplication.AuthorizeRequest event. ASP.NET MVC is still subject to the ASP.NET HttpApplication life cycle.

Categories

Resources