Using Swashbuckle to Host an Existing Swagger.JSON file - asp.net-mvc

I have an existing Swagger.json file for a .NET WebApi site which I would like to integrate with my developer site (which is at a different url) running Swashbuckle.
Can I simply copy this swagger.json file to the mvc site (content or app_data folder and do this?)
If I can, where is the best place to put it.
How do I make this the default swagger file served up by the host?

The EnableSwagger method has two signatures, in one of them you can specify the route template to your files, so you can really serve it from wherever you want, just make sure your template matches the location of your json file. Take a look here: http://pastebin.com/MDfWNpUs

Related

How to serve some static files of a certain file type, but not others?

My ASP.NET MVC project's root directory contains some typical static files, like robots.txt, manifest.json, browserconfig.xml, etc. If I'm not mistaken, each of these examples I listed should be able to be served with no involvement from MVC via GET requests to the root directory (i.e. mysite.com/manifest.json — if that's not true, please let me know).
I know from this answer that I can configure this behavior per file type in the Web.config. My question is, what if there are other .json files in my root directory that I don't want to serve, like compilerconfig.json or bundleconfig.json (both files generated by IDE tools)? What's the best way for the application to be able to serve some files of type X, but not others?
You can always ignore them via routes:
routes.IgnoreRoute("{somefilename}.json");
Another alternative would be to move the files you don't want to be served to another folder and add a web.config file to it to manage what gets served (or doesn't).
I am sure there are other ways. Modules come to mind...

How to configure rout to serve all files from subfolder using httphandler in asp.net mvc?

I have two websites in IIS under default web site. Lets say "Website1" page gives call to "webiste2" which return some content. Entry URL of website2 is http://domainname/websitename/controller/action this action return html(index.html). structure of website2 is as bellow.
Website
Folder(websitename)
........Folder A(folder_{id}(id is dynamically generated) is under folder A)
..............Folder_{id} --> this folder contains files with many extensions
........Index.html->this file has references to above folderA &subfolders files
........Web.config
Now i want to configure route such a way that that serve all types of files extensions for url like below.
websitename/folder A/filename.abc
websitename/Folder A/folder_1/filename.xy
websitename/folder A/Folder_1/filename.mp3
websitename/folder A/Folder_1/filename.png
There can be thousands folder under folder A. so the folder_1 value can be changed from folder_1 to folder_2, folder_3 .............. folder_1000 or as many folders.
I want generic rout to serve all dynamically generated folders and its files.
There are many files with different extensions under these dynamically created folder or we can see its as package of different files like .mp3, ,jpg, .swf, .js...
Please guide me to write rout to achieve above. Or you can provide links so that i can go through.
Simply, don't. Static files are supposed to be served directly by IIS. By default, in MVC, anything that has an extension (.*) is handled by IIS, and MVC is never even involved. If you've customized something, I'd recommend undoing that.

how to serve static files via MVC after login (I am trying to add an angular mini-SPA to my large MVC project)

Looking for suggestions on how to host static files through an MVC app.
I have an MVC SPA (basically a bunch of static html, js and img files). I want users to be able to access these static files only after they have logged into my MVC application. I am running on a windows server platform, using IIS.
Currently I am doing this:
RouteTable.Routes.IgnoreRoute("AngularApp/{*path}"); //to serve up angular files from AngularApp folder
However this has a number of problems.
I don't really want to ignore the route, I want the MVC controller to check if the user has permissions (like my other controllers do), if not redirect to login page and if so, then instead of sending them to a view, allow them to load any files in a particular folder or subfolders. But the folders these files load from need to be a different path than the route URL requested. For example I don't want users to have to go to mysite.com/angularseedapp/deploy/app/mypage.html but rather if they request mysite.com/a/mypage.html I want it to serve up the file from there.
This seems simply a matter of being able to have MVC redirect and fetch files from a different folder, but I have no idea how to do this.
Could someone knowledgeable about MVC please give me a step by step simple way to do this? When I try to fetch files outside the views folder this seemingly simple task results in various permissions and other kinds of errors because I don't know how to do it correctly.
Thanks!
P.S. To clarify, I know how to get my controller to check permissions and redirect, to any single file in the views folder, but how do I do it for a whole folder of files and directories in a higher level folder? I want to map the route, have it go to a controller, then instead of going to a view I want it to take me to static files. I suspect there is some way to use maproute() in global.asax to help me do this but I do not have a lot of experience with that.
I may be oversimplifying but I usually select the application in IIS Manager and then select Mime Types, they add mappings for whatever types you want to map statically. I've done this for HTML and JSON files before and it worked fine. Use type = text/javascript or application/json etc.

How do I serve static files from mvc without using content folder?

I want to be able to have a folder which allows regular access like the \content folder except that it holds a ClickOnce application. I can't seem to be able to achieve this using Mvc, but I'd like to have this folder accessible without Mvc seeing it as a controller action.
I tried using routes.Ignore(theUrl), but this seemed to have no effect.
There are two ways you can do this. The first is where you are currently going, which is to satisfy it with routing. You should be able to use the following to ignore the intended route:
routes.IgnoreRoute("...")
However, this might not be the right approach from a security stand point. I would recommend you define an explicit action to download your click-once exe. Have a look at this q/a as an example of using the FileContentResult class.
The reason for this is that you can control security for that file without having to open up access levels to other directories.
Edit: If this is for an entire directory, you can still follow this same approach.
Set up the folder as a virtual folder in the website on IIS. then you can set the url in the code to point to the machine serving the request and to the virtual folder on the web server.

ASP.NET MVC routing and static data (ie. images, scripts, etc)

If I have a request for a resource in my ASP.NET MVC1 (or 2) web app for a static resource, say ... an image or a javascript file or a css file ... does the .NET framework try and see if the request matches the route list ... and eventually can't find a controller for it?
eg.
Resource: /Content/Images/Foo.png
Does this request go through my route list .. fails to match any controllers / actions to this request and then attempt that path directly?
You can choose whether to map an existing file or not setting the RouteCollection.RouteExistingFiles Property
Gets or sets a value that indicates
whether ASP.NET routing should handle
URLs that match an existing file.
Here is what I read from here:
However, the routing system still does check the file system to see if an
incoming URL happens to match a file or disk, and if so, routing ignores the request (bypassing
any route entries that the URL might also match) so that the file will be served directly.
This is very convenient for static files, such as images, CSS, and JavaScript files. You can
keep them in your project (e.g., in your /Content or /Script folders), and then reference and
serve them directly, just as if you were not using routing at all. Since the file genuinely exists
on disk, that takes priority over your routing configuration.
If, instead, you want your routing configuration to take priority over files on disk, you can set
the RouteCollection’s RouteExistingFiles property to true. (It’s false by default.)
By default the routing engine will ignore route maps for all files that exist physically on the server. In short, you need to do nothing for a MVC app to link to static files.
You can also do a little trick in IIS. I store my js, images, css etc in the Content folder underneath the virtual directory.
If you then view properties (in IIS manager) of the Content folder, create it as a virtual directory, then remove the Wildcard mapping. Then set the Content folder back to a normal directory. This should then stop requests to these files being handled by the aspnet_isapi handler.

Resources