I have a virtual directory under my MVC website in IIS called "Files". This directory is at the same level as my Views directory. When I link to a file from my MVC app to a file under my Files directory, I get the following error:
The controller for path
'/Files/Images/1c7f7eb8-5d66-4bca-a73a-4ba6340a7805.JPG'
was not found or does not implement
IController.
It thinks that my Files VD is a controller. How do I access my files like a normal VD without MVC interfering?
Thanks.
When registering routes, try to add the following Ignore rules.
public static void RegisterRoutes(RouteCollection routes)
{
/* Ignore static content, see
http://weblogs.asp.net/rashid/archive/2009/04/03/asp-net-mvc-best-practices-part-2.aspx
*/
routes.RouteExistingFiles = false;
routes.IgnoreRoute("Content/{*pathInfo}");
routes.IgnoreRoute("Scripts/{*pathInfo}");
routes.IgnoreRoute("Styles/{*pathInfo}");
routes.IgnoreRoute("{*favicon}",
new { favicon = #"(.*/)?favicon.([iI][cC][oO]|[gG][iI][fF])(/.*)?" });
//Ignore handlers and resources
routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// your routes go here
}
ASP.Net looks for the directory first and then tries to match a controller, so what you are doing should work. Are you sure the file with that name exists and is accessible?
I think you'll have to add a call to routes.Ignore() a static route in your Global.asax file so that .NET MVC knows to ignore the request:
RouteCollection.Ignore(String) - MSDN
Related
I am having a curious issue with one of my projects in development. The issue is with links to a certain URL "//localhost:62168/Images/Index". I have buttons linking to that URL but when "//localhost:62168/Images/" is accessed it returns a HTTP Error 403.14 - Forbidden error. See the below Image:
localhost//Images/
Oddly enough though, when I enter the URL exactly ("localhost:62168/Images/Index") it loads the page properly. See the below Image:
localhost/Images/Index
I've done plenty of research online and I believe it may be an issue with routing so below I've added the code of my "RouteConfig.cs" file. Unfortunately, I am new to ASP.net and MVC and not knowledgeable on proper Routing procedures:
using System.Web.Mvc;
using System.Web.Routing;
namespace ReedHampton
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
I've also tried multiple other "solutions" presented in other online forums to no avail. These include:
Adding "directoryBrowse enabled="true" " to web.config
Running "C:\windows\Microsoft.NET\Framework64\v4.0.30319> aspnet_regiis.exe -i" in Command Prompt
Going through IIS Express and registering IIS Express and ASP.Net
I would greatly appreciate any help that anyone can provide!
IIS will attempt to short-circuit the request if it finds something on the filesystem that matches the URL. In other words, I'd assume you have an Images directory in your document root. Therefore, IIS will attempt to hit this directory, rather than pass the request on to the ASP.NET machinery. Since you've disabled directory browsing, you get a 403.
Long and short, you need to keep your ASP.NET MVC routes unique from what you have physically in your document root. You could change the name of the Images directory to something like img, or put it in a parent folder like the default of /Content/Images. Otherwise, you'll need to change your controller name or create a custom route to that controller not called /Images.
I know that removing URL extensions is the new model for website programming. Unfortunately, my site is hosted on a hybrid server configuration. The call to my site goes into an Apache server that recognizes that my call is for a .aspx page, and passes the call along to an IIS server to complete the call. This complicates my website at this point because I am coding in Visual Studio 2015, and it models after the new rules of removing the extensions, and the call is never passed along to the IIS server.
I am not a big HTML guy, and I cannot find anything to place in web.config or my global.asax file for code to tell the system to overwrite the rule of removing the extension, and to keep my extensions. I have seen several posts here to remove the extensions, but nothing to keep them.
Basically, when I call www.mysite.com/Default.aspx, the current config removes the .aspx extension, and the call is for www.mysite.com/Default. I want to KEEP the .aspx extension on the call to the site so that it passes through the Apache server and to the IIS server. Any help would be greatly appreciated.
Have a look in your App_Start directory for a class called RouteConfig.cs and disable AutoRedirectMode using this line of code
settings.AutoRedirectMode = RedirectMode.Off;
This is what automatically removes extensions from your web pages.
Full example below...
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Off;
routes.EnableFriendlyUrls(settings);
}
}
I have an ASP.NET application, and I am trying to turn this into a hybrid ASP.NET / ASP.NET.MVC 4.0 application.
I tried to create a folder named "Controllers", and place a .cs file in there:
public class PlayerGroupController : Controller
{
public PlayerGroupController()
{
}
public string Index()
{
return "Hello World!";
}
public ActionResult LayoutTemplates()
{
return View();
}
}
Any attempt to access "PlayerGroup/LayoutTemplates" doesn't work (just get a "Not Found" error)
I then moved this file into App_Code, and it works fine. I'm glad I got something working, but I would rather follow the convention of controller classes being in the folder named Controllers.
Is there some magic setting I can set somewhere so that it starts recognizing Controllers as a code folder?
If the project is configured as a Web Site Project then all code files (*.cs) must be in ~/App_Code or in subfolders thereof. If the project is a Web Application Project, code files can go anywhere in the project and be compiled by VS into a DLL that ends up in the ~/bin directory, which then gets loaded by ASP.NET.
MVC is geared specifically towards the Web Application Project (WAP), so I recommend creating a new WAP and copying all the files into that, and then going from there.
We need a way to say
www.site.com/india
www.site.com/asia
www.site.com/usa
etc...
But we want all these requests to point back to www.site.com and not have to physically create the files and directories for every type of site... so if I just create a virtual directory (www.site.com/india) and point it to www.site.com... then I figure I can look at the URL and set some parameters/text/images accordingly to fill out the template...
Does this make sense? Or are there issues with web.configs ? OR a better way
I would encourage you to consider Routing, as demonstrated here: http://www.asp.net/mvc/tutorials/asp-net-mvc-routing-overview-cs
The given sample, repeated here so you can (hopefully) see the relevance, is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MvcApplication1
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
By similar methods, you can merely extract the information you need
The first example was for MVC Routing, to do webforms routing you should look here http://msdn.microsoft.com/en-us/library/cc668202(VS.90).aspx and here's a (very limited) example:
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add("BikeSaleRoute", new Route
(
"bikes/sale",
new CustomRouteHandler("~/Contoso/Products/Details.aspx")
));
}
Using a custom routehandler that they build in the code. Routehandlers are easy to use, once you get the hang of it. And they allow you more expressiveness.
I think you'll find that localizing the same site five ways without having different web.configs pointing you to your localization files will be difficult.
What I would do is have www.site.com's index wither automatically discover the client's location or ask them what version of the site they want. Then, refer them to a default.aspx under that site's virtual directory, that uses a common set of your custom web controls, localized according to the resource file referenced in that site's web.config. That way, all you'll need to copy is the site skeleton, which can be as little as the index.aspx.
If the site is mostly aspxs, this isn't going to work as well. In that case, I would do the website selection as before, but use a query string to keep track of client location and thus localization. That should still allow you to bring up the correct localization settings.
No, please promise me not to create a virtual directory for each country in the world. Checkout routing instead. And here are some other examples.
Consider using routing or even better MVC if you haven't written the sites already. You should definitely NOT be creating multiple virtual directories - it would be a nightmare to maintain
I want to protect the files in a specific directory so that people cannot link to them - but I still need my program to be capable of rendering them, so password protecting the actual folder won't work. Is there any way to facilitate this in ASP.NET MVC? Obviously it's more about ASP.NET, not MVC - but I can't make use of things like Session State and Postback and such...
You could put that directory outside of the web app's root directory (so that it can't be accessed using some copied URL) or into a directory where you deny any read access using a web.config file.
Then access the files through an action which requires the current user to be authorized, e.g:
public class FileController : Controller
{
[Authorize]
public ActionResult Get(string file)
{
return new File(Path.Combine(_rootPath, file);
}
}
Then you can access the files through an action URL, e.g. http://server/app/File/Get/test.txt.