I am creating an MVC application that will effectively allow a user to upload a number of Themes to the site.
Each theme they upload will have a _Layout.cshtml file, along with css files and images.
These will all be stored in locations mapped to the userId of the person creating the uploads, i.e.:
Users/Themes/32-bit-guid/_Layout.cshtml
Users/Themes/32-bit-guid/css
Users/Themes/32-bit-guid/images
The problem I have, is that although the templates are uploaded and I can select any of the templates (which are then applied to the current logged in user), the CSS files are ignored, along with the images.
If I try to browse to the path Users/Themes/32-bit-guid/css/screen.css which I know exists, I get a resource cannot be found error.
I have had a look at creating custom view engines, ignoring routes and everything, but I am getting no where.
Has anyone experienced this before and can they point me in the right direction please?
Views in MVC require a Web.config, all I did to get the themes working was copy the default web.config file from any Views folder and put it in the same directory as my Layout.cshtml
Related
So we've got a project that our tutorial software exports as an HTML project. (Root has an index.html file, folders for js, css, etc)
Our support portal with Identity account management runs on ASP.NET MVC.
What I'd like is for the HTML project to be accessible, but you must login first.
I've gone about it two ways, unsuccessful both times:
I can put the HTML project in a static folder in the root of the ASP project. This way I can access it as domain.com/Test/Course/index.html. Everything works fine here, js and css are loaded properly, but it is not password protected.
I can put it in the App_Data folder, create a route to an action with the [Authorize] parameter that returns a FileResult, grabbing from the App_Data folder. This locks, and on login loads the index.html file, but all of the html files resources (accompanying js and css files) fail to load because the paths are wrong. It's pulling from App_Data, but index.html is now somewhere else, away from it's included js and css folders.
Any ideas would be greatly appreciated.
You'll want to take a look at these two questions:
How to do Forms Authentication on purely HTML pages using ASP.NET?
https://serverfault.com/questions/509879/protecting-static-content-with-forms-authentication-under-iis8
Basically - ASP.NET doesn't by default handle .html content because it's much faster to have IIS handle it directly. You need to configure it correctly to have it apply to .html files so that it can then apply its authentication model.
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.
Trying to integrate BlogEngine.NET into an existing large and complicated .NET web site, I've found that my cshtml files cannot locate the helpers files. This is a site which has not previously made any use of MVC, and my own experience with MVC is modest enough that I had to review to even remind myself what helpers were.
I placed the BlogEngine.NET code in its own subdirectory.
Now the code in "[rootdirectory]/Blogs/default.cshtml" needs access to the code which now resides in the "[rootdirectory]/App_Code/Helpers/" directory.
I haven't changed the code trying to do this, ad representative sample of which looks like: "#Helpers.ExtensionsHelper.GetExtensions(true)".
I've tried moving the Helpers directory and its contents around extensively. I've tried adding assemblies to the web site to include all the assemblies that appear in BlogEngine.NET.
I always wind up with the error, "The name 'Helpers' does not exist in the current context".
I am just new to MVC, we are building a massive system and have alot of namespacing in our site.
Where is the standard place to store files (CSS, Images, .JS) etc?
Would it be good to put them under the Content folder in sub-folders to their namespace or drop them in with their respective parent files or both.
The default project structure includes a Content folder for CSS files and a Scripts folder for JavaScript files. A lot of people use this existing template, especially since a lot of Nuget projects may rely on this.
I personally like to put all of the content in a Content folder, and have a subfolder under this named Css and Scripts. It's really a matter of preference though. Do whatever is consistent and well-organized. That will be the key to making the application more easily maintainable.
Its better to put it in saparate folder as Script(js file) at root level.
Add sub folder in content folder for Images and Css etc.
we can also create multiple controller and views for each section of your project.
like for login section you can add Authentication Controller.
we can also use Helper class for adding general function and use it in every where.
you can see following link
http://msdn.microsoft.com/en-us/library/dd410120(v=vs.100).aspx
I will like add conventional HTML page under VIEWS folder (in ASp.NET MVC) page.
I have added the route exceptions as mentioned below.
routes.IgnoreRoute("{resource}.htm/{*pathInfo}")
routes.IgnoreRoute("{resource}.html/{*pathInfo}")
Although it does work when I put the html files out of VIEWS folder but I get Page not found 404 when I put those in VIEWS folder. I am also unable to browse the VIEWS folder by setting directory browsing option in IIS.
Please help me on HOW to access HTML file from VIEWS folder.
I think that it's a mistake to mix your HTML content with your views. I'd suggest that you create a separate static folder under Content and put your HTML there. You can create an analogous directory structure to your view structure if necessary for management. Then you don't need to do anything special in order to able to reference the files. You can even, then, open them up to editing with Contribute, etc. by people who are allowed to modify static content.
+-Content
+-Images
+-Static
+-Account
+-privacy.html
+-refunds.html
+-Styles
Usage:
<a href='<%= Url.Content( "~/Content/Static/Account/privacy.html" ) %>'>Privacy Policy</a>
The default Views folder has an Web.config file that explicitly gives 404 errors for all requests. You just need to edit and enable for HTML files (or all files, but then people might snoop).