Can I use domain-specific wwwroot folders in my project? - c#

So I've been studying the documentation of.net Core Middleware and I'm working on a minimal Web API with an additional wwwroot folder containing static HTML content with CSS and Javascript. This is basically a demo for the Web API in the same project. The JS in this folder calls some of the API methods in it to provide various content.
But I want to use this API with several different domain names so they all share the same API. But the domains would each need their own wwwroot with static pages. And my Web API will use app.UseWhen() combined with the domain name to have some domain-specific methods per domain. This would allow me to have one site supporting multiple domains. This filtering based on domain names should be simple, but they would all share the same wwwroot. I want each domain to have it's own root.
Now, UseWebRoot() seems to solve this, except it gets called from the builder, not the app. This means I only get one where I want many.
It seems that app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "DomainName")) }) would work. (ContentRootPath, not WebRootPath, as I want the wwwroot folder to stay clean.)
Is this the proper approach to have custom static pages per domain?
These static files in the wwwroot will not be under control of the project itself, but are generated by another team member separately. They are front-end developers who just make things look more fancy per domain. (And in different languages where need be.) And while these sites could be different sites, I want to know if it can be contained inside one project.

Related

How to call WebAPI from a MVC Controller in a different project?

I have to create a Web API for my existing MVC project and make API Controllers calling the Service Layer and Models, which is contained in separate projects but in the same Solution, then create and map to DTOs.
Layout of my Projects Solution in VS
( ignore the BookService.cs. I was just trying to do a WebAPI tutorial and I put it in the same solution temporarily).
I have been reading up on WebAPI and how it functions for the past 2 days, but I am not being able to fully grasp an understanding on how to create a API Controllers for my MVC project without referencing it? I also have to make Views at the end, in my main project calling the uri, but I am very confused at this point.
It would help me out a lot if someone can please clarify how I am to tackle this or point me to a tutorial or some sort of source to learn the process of working with Web API. Thank you.
The Web Api project will be a separate "website", that you will need to host individually. Your MVC project will make requests to the Web Api using HttpClient.
Since the Web Api will be separate, you won't be able to utilize helpers like Url.RouteUrl, etc. to get URLs for the Web Api actions. You will also just need to know the full URI to the Web Api, including it's domain. There will be no way to programmatically ascertain this information, so I would recommend making use of Application Settings to avoid hardcoding in your MVC project.
Right-click on your MVC project in the Solution Explorer and choose Properties. Then click over to the Settings tab. Here, you can add strongly-typed settings that your MVC application can utilize. Importantly, these settings are still persisted in the Web.config, so you can change them using config transforms. Your Web Api will likely have different URLs depending on whether you're in development vs. production, for example, so that will make it very easy to ensure that you're hitting the right thing in the right environment.
You can add a setting like WebApiUri, and give it a type of System.Uri. Then, set it to the string value of where your Web Api is hosted in development, i.e. http://localhost:12345. It's important that the setting be specific to your development environment, as config transforms are not applied in development. For staging, production, etc. you'll change this setting appropriately in the applicable config transform, and it will be updated to the right value for the right environment when you publish.
Then, when you need to work with it, you'll just do something like:
var client = new HttpClient();
client.BaseAddress = Properties.Settings.Default.WebApiUri;
Then, just make requests as normal through the client. For more information on working with HttpClient, see the documentation.

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.

Subdomain rewrite to static file directory in ASP.NET

I am trying to achieve the following in an MVC5 app.
In the MVC site root, the is a subdirectory "Sites" where static files live. For instance, there might be static assets (HTML, JS, CSS) for "acme" and it is located under "/Sites/acme/"
Ideally, if you hit "acme.localhost" (in my dev environment) it will serve the contents of the /Sites/acme location, defaulting to index.html. So basically everything on the subdomain of acme will be rewritten to be relative to /Sites/acme. I have the DNS side of this all sorted out.
I know the IIS URL Rewrite module is designed to do this scenario, however the catch is users can define their subdomain in my MVC app dynamically (these are currently stored in a database). I don't know what they will be, and as far as I understand editing the rewriting in web.config if I were to do it this way will cause the parent MVC application pool to recycle. This makes me think it has to be some sort of HttpHandler that looks at a cached table of possible subdomains registered in the system and rewrites accordingly.
Any thoughts or suggestions? I was thinking a HTTP module or custom Route handler but not sure how to achieve this, and am very wary of doing this in an efficient way.
Thanks

Serving shared media / content securely

I have a few MVC4 websites that share some of the same images / videos / pdfs etc. They are confidential: that is only authorized users can access them.
At the moment I just have the content in a folder under one of the web apps, and then I create a symlink to that folder from within the other web apps so that they share that directory. I don't want to do this because it makes things complicated for testing and deployment and would rather have some kind of CDN type of website to serve it.
What's the best practice here?
I guess you've answered your own question. Try using a CDN instead of having them inside your server(s) and shared across your other web apps. Some CDN's may or may not have authentication.
One CDN you might want to consider:
Amazon S3 (it has token auth)
Another is Softlayer (this also has auth)
If you really intend to create a CDN-like website, I would say, do a cost-benefit analysis. Is it worth to build it from scratch? Can you just get a CDN (with authentication, of course) and host it by yourself? Or can you just have it hosted externally (which might be more reliable as well)?
Just my 2 cents.

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.

Categories

Resources