I'm working on an ASP.NET MVC app that uses a Flash movie as a banner. I'm trying to load it, however for some reason the url gets treated as an Action and the user is redirected to the login page.
The funny thing is, if I put a file in the same directory with a different extension (.txt, for example) and try to load that, it doesn't get treated as an Action.
If I point my browser to this url:
http://localhost/AppImWorkingOn/Content/Banner/banner.swf
That gets me a redirection to /Account/LogOn?ReturnUrl=..., the standard ASP.NET MVC login route.
If I point my browser to this url:
http://localhost/AppImWorkingOn/Content/Banner/banner.txt
That gets me the file. Both urls point to files existing on the server in the same directory. It's as if something is blocking the .swf filetype.
I'm using IIS on Windows 7, and checked MIME and such, but can't find the problem. How does ASP.NET MVC decide whether something's a file or a controller action?
Make MVC ignore the route for the ".swf" extension.
http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx
The issue was file permissions. This was a file that came from a zip sent to me via e-mail. I dragged the folder out of the zip and moved it into place, which was probably the worst I could do. The files were individually blocked and encrypted (they turned up green) so the UrlRouteModule wouldn't treat them as files that existed and would instead insist that they're Action methods.
I should have "unblocked" the zip file before starting to copy the assets in it.
Related
I have a rewrite rule that says.
site.one/content/templates/whatever.cshtml -> site.two/content/templates/whatever.cshtml
The problem occurs when site.one trying to fetch .cshtml files. It returns a 404.
However if i change the extension to .js or .html then the rule works.
If i use the site.two link the request handles it just fine, this is without any rewriterules ofcourse.
site.one can also handle .cshtml files from within its own directory.
Iam using IIS 10, Urlrewrite module 2, and Application Request Routing 3.
UPDATE
So the .cshtml in this case are template files that is fetched with async text.js, and then gets compiled with handlebars as the last step before rendered, but some of them need to get processed by razor engine because of the translations. This works if not rewriting.
Razor files are never served by the MVC framework or IIS. Razor files are processed by MVC and the result of the processing is sent to the output through a controller action method. The server is correctly responding with a 404 response in this case, because these files are blocked by default (you would never want to serve an unprocessed .cshtml file with source code to the user).
Since these files are blocked by IIS, there is no way to redirect them using the URL Rewrite Module (nor should there be).
I am guessing that you probably want to redirect your MVC URLs, which don't have an extension by default. The default way they are accessed (which can be customized) is /{controller}/{action}/. For example, the in the MVC template the default way to get to the about page is through the URL /Home/About/.
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.
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.
Can't make this work fast. I have a folder in which I have text files.
And so if user requests some file like mysite.com/thefolder/file.txt he sees the contents of the file.
What route should I register so that all requests to anything in the folder would go to mysite.com/Error/NotAllowed action?
IIS will, by default, serve all .txt files directly without even going to the ASP.NET handler, which handles routing and MVC.
To change that behaviour, you will have to change your IIS settings or change the routing rules at any firewall/load balancer you have in front of it.
What is the best way to get hosting of an ASP.NET MVC application to work on IIS 5 (6 or 7). When I tried to publish my ASP.NET MVC application, all I seemed to get is 404 errors. I've done a bit of googleing and have found a couple of solutions, but neither seem super elegant, and I worry if they will be unusable once I come to use a shared hosting environment for the application.
Solution 1
Right-click your application virtual directory on inetmgr.exe.
Properties->Virtual Directory Tab-> Configuration.
Add a new mapping extension. The extension should be .*, which will be
mapped to the Executable
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,
or the appropriate location on your
computer (you can simply copy this
from the mapping for .aspx files). On
the mapping uncheck "check that file
exists".
3 X OK and you're good to go.
If you want, you can apply this setting to all your web sites. In
step1, click on the "Default Web Site"
node instead of your own virtual
directory, and in step 2 go to the
"Home Directory" tab. The rest is the
same.
It seems a tad hacky to route everything through ASP.NET.
Solutions 2
Edit the MVC routing to contain .mvc in the URL and then follow the steps in solution 1 based around this extension. Edit: The original image link was lost, but here it is from Google's Cache:
Answer is here
If *.mvc extension is not registered
to the hosting , it will give 404
exception. The working way of hosting
MVC apps in that case is to modify
global.asax routing caluse in the
following way.
routes.Add(new
Route("{controller}.mvc.aspx/{action}",
new MvcRouteHandler()) {
Defaults = new RouteValueDictionary (new{ controller
= "YourController"} ) });
In this way all your controller
request will end up in *.mvc.aspx,
which is recognized by your hosting.
And as the MVC dlls are copied into
your local bin , no special setttings
need to be done for it.
I think either way you'll have to do Solution 1.
Consider the HTTP Request pipeline.
A request comes into IIS.
IIS checks port/host header to see if it has a web site set up to capture requests for that host header/port.
IIS investigates the file extension of the request (.php, .asp, .aspx) and hands it off to an ISAPI that can handle that type of request.
Only at this point does ASP.NET (or a PHP runtime) kick in. If IIS does't have that mapping then it'll never hand off the request to the ASP.NET runtime and the request will never reach your code. That's why you need that glob (*) mapping to the ASP.NET ISAPI.
ASP.NET MVC framework urls often end with no file extension at all. If you want these requests to get handled by ASP.NET (or some other runtime) you have to map all requests regardless of the file extension to that ISAPI (ie. aspnet_isapi.dll).
This is often also done for HttpHandlers that need to serve off media like .jpg, .gif. For the handler to be hit it needs to get mapped to your code even though .jpg isn't a "normal" ASP.NET file extension.
HTH,
Tyler
Run:
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll -i
This will reset IIS registry settings for aspnet user.
Create the virtual directory:
1. Right click on the directory you want to convert
select Properties
under Directory, select Create.
under Configuration, select Add.
for Executable insert:
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll
for Extension insert: .*
uncheck “Check that file exists”
under Documents add entry point file, ie: Default.htm, index.htm, Global.asax
under Directory Settings
select Edit...
select Anonymous access
uncheck Allow IIS to control password
uncheck Basic authentication
uncheck Integrated Windows authentication
under ASP.NET, make sure version = v4.0.30319
TAKE NOTE of User Name ie: IUSR_AVSJ82S
Set sharing permission of physical directory:
In windows explorer, go to the physical directory that was converted to a virtual directory.
Right click the directory name
select properties
under security tab, select Add
enter the IIS User name ie: IUSR_AVSJ82S click check name.
click OK
set permissions to Read and Write.
FYI:On server 2003 (developing an app that had to connect to the RPS), it didnt' allow me to add the extension .*, I used the alternate solution modifying the route clause, and that
worked.
Have you tried adding .aspx to the end of the controller name?
It worked for Stack Overflow question Where can I get ASP.NET MVC hosting?.