ASP.NET MVC: how to bypass the controller in MVC app? - asp.net-mvc

I have a .NET MVC application. So far, each page is accessed via a controller.
Now I want to direct access some cshtml files such as
http://example.org/file/abc.cshtml.
Though having .cshtml file extension, these are just pure html snippets.
How can I access these files without going through any controller.
Thanks and regards.

I would probably make a controller with an action which accepts a view name, grabs the view from the file system, and returns it as a FileResult with the mimetype text/html. You'd probably want the controller to have a hardcoded whitelist of html-fragment files, to reduce the chances that you're opening up a way for people to browse around your folder structure.
You could also look into configuring IIS to serve .cshtml files from some directories, but I'd be more concerned about accidentally opening up too much using that method.

Related

AngularJS + ASP.NET MVC: routing URLs without corresponding physical view

Context
We have a web app using AngularJS with ASP.NET MVC. It aims to replicate a file-system structure through URLs, like this:
http://(site1)//rootFolder//NFolders//File
For views in Angular, we are using stateProvider, with different states associated to existing views in disk (.cshtml files).
The problem issues when we request a file or folder from the browser and we try to serve a virtual view that doesn't really exist. We have just been able to achieve it with a full postback and some hacks, making a complete reload on each file/folder request.
Question: Is there a way of achieving that in Angular? Other way, the request for different URLs which don't have either their corresponding physical view as a file or any state in stateProvider, handling them with Angular and customizing a specific view which would deal with disk routes requested by URL?
RE-EDIT
We will have:
a fixed root view for the website: http://website
several root projects with a name:
http://website/site1
http://website/site2
....
http://website/siteN
each site may have different files and folders accesible via the URL:
http://website/site1/folder1/folder2/.../foldern
http://website/site1/folder1/folder2/.../foldern/fileX
RE-QUESTION: is it possible to handle this "virtual" URLs without having a physical view behind for folders or files? Our idea is to have one unique view called siteController which would control all the sites, and paths to folders and files.
I would suggest you define rootFolder/NFolders/File as parameters. Then you can have a html file as the view template to populate data from server.
Have a look at this link
https://github.com/angular-ui/ui-router/wiki/URL-Routing#stateparams-service

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.

ASP.NET MVC Displaying Static Help Website

I'm trying to add Help to my ASP.NET MVC project.
The "help" website contains static pages about the features in my ASP.NET application.
I have added the content for this website into my ASP.NET MVC project and have added a hyperlink that will open the Help in its own window.
However, when I try to access the content, the application attempts to route to the Help controller.
How do I display the help website within my MVC application?
I am not sure you can do this within the context of an MVC application. I would consider just building an empty controller with an Index action (HelpController -> public ActionResult Index()) and just return the view name (cshtml file), shouldn't be any reason you can't rename your static html file to cshtml even if you aren't using razor (although I am not 100% sure without trying that the extension change is necessary). Also I would argue that if this ever needs more functionality you have the scaffolding in place to make non-static mods. Disabling routing within the context of an MVC solution honestly doesn't make the most logical sense. The only other choice would be if you hosted it in a different IIS site (but I don't think I would recommend that unless you have a huge help library).
Use IgnoreRoute when you configre your routing, for example, create a folder "help" in your app's root. Then load it with all your html help files. Then to ignore that route:
routes.IgnoreRoute("help");
You should then be able to access it by http://myapp.com/help/whatever.html

use of partial views (file upload forms on different pages)

I am very new to asp.net MVC.
I need to create dnd file "component". It allows user to upload file to server using DnD, the upload is done via webAPI. I am using FileModel here, for example, webApi also returns FileModel (to show uploaded file info on page). I did this part.
However, I did it in the "TestView". I need ability to add this "component" to any view on my project. Unfourtunately I do not have knowledge enough to do this.
I have found out that there are PartialViews. Should I just move my "TestView" to some "FileUploadPartialView"? What problems this will cause? How this affect the usage of FileModel?
Partial Views are like a javascript include. You shouldn't have any trouble.
Just put this wherever you need to have your partial view...
#Html.Partial("_FileUploadPartialView")
Let me know if you need more detail.

.NET MVC - accessing .xhtml file

I have created an Area for XForms and when I try to return view("index.xhtml") the framework resolves the view as index.xhtml.aspx or index.xhtml.cshtml.
I tried routes.IgnoreRoute("{resource}.*xhtml/{*pathinfo}"); in global.asax.
Either I am not sure what URL to use (am I still hitting the controller or going straight at the .xhtml file in the views folder?) OR I made a mistake in my ignoreroute.
Any help appreciated.
If you are trying to have the action just write the content of index.xhtml, you'll need to do return File("index.html", "application/xhtml+xml"). View/PartialView assume you want the specified view file parsed and executed using the currently configured view engine.
You can't/shouldn't put static files you want remote users to be able to hit directly in your ~/Views folder. MVC places a web.config file in this folder that prevents files in this location from being served.
So, either have your controller action return the file as I mentioned above, or move the xhtml files into some other folder in your application that is not restricted. Then your route should work and your files should be served statically.

Resources