1

When my clean, just created new app (.net 4.0 integrated) is on Visual Studio Web Server everything works fine. Link like this below works fine and controller returns image.

http://localhost:12345/image/a.jpg

But when I run this app on IIS 7.5 then iis takes control and reports 404.

http://localhost/testmvc3/image/a.jpg

Controller:

public class ImageController : Controller
{
    public ActionResult Index(string name)
    {
        var dir = Server.MapPath("~/content/" + name);
        return File(dir, "image/jpg");
    }
}

Routes:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Image", // Route name
        "image/{*name}", // URL with parameters
        new { controller = "Image", action = "Index", name = UrlParameter.Optional } // Parameter defaults
    );

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}

What I should change to run this app properly?

EDIT1:
The problem is with extension. When I remove extension then requests points to image controller. With extension (jpg) iis takes request first (why!?) and returns 404 (without touching image controller action).

EDIT2:

  • IIS 7.5 on Windows 7 64bit
  • App on Framework 4.0 integrated pipeline

web.config before my changes:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
dariol
  • 1,959
  • 17
  • 26

4 Answers4

1

I found it. Uchh.....

I change ExtensionlessUrlHandler path from default '*.' to '*':

<handlers>
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <add
        name="ExtensionlessUrlHandler-Integrated-4.0"
        path="*"
        verb="GET,HEAD,POST,DEBUG"
        type="System.Web.Handlers.TransferRequestHandler"
        resourceType="Unspecified"
        requireAccess="Script"
        preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

Now all requests are going through routing engine.

Then I add a IgnoreRoute to 'content/{*all}' where I have all static content files.

dariol
  • 1,959
  • 17
  • 26
0

If the solution is deployed, that is, not only mapped against your solution but instead deployed as it should be, the result of Server.MapPath will not be as you expect. The Server.MapPath "default" folder is where the actual dll is.

One way you can solve this on is to set the "Copy to Output Directory" for your images to "Copy alwats" or "Copy if newer".

You could maybe use something like Server.MapPath("~/content" + name);, but I that will make your solution not finding the images.

Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
  • The problem is with IIS which taking first the route and matches it to file that doesn't exists. Why IIS takes control over my application? – dariol Oct 21 '11 at 12:09
0

In the Properties for your project, on the Web tab, in the section below the radio button Use Local IIS Web Server, in the box for Project Url, change the entry to http://localhost:12345.

Then, in IIS, edit the http binding for the localhost site, and change it to 12345.

counsellorben
  • 10,924
  • 3
  • 40
  • 38
0

you should not be required to do that. Make sure your application pool is running in Integrated pipeline mode.

Cosmin Onea
  • 2,698
  • 1
  • 24
  • 27
  • Sorry, your first sentence was talking about the dev server, and I missed that. I tried your scenario and everything worked out of the box. When I switched to classic pipeline I got the 404 error you are describing – Cosmin Onea Oct 22 '11 at 14:07