Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke ASP.NET MVC Controller When Requesting .html file

I need to add some new life to a legacy application :)

I'd like to call an MVC controller when a "static" HTML page is requested in order to add some markup to the page before returning it to the client.

I tried to follow the approach found in this thread: How to read web.config settings in .html page?

...but even though I have this route defined:

routes.MapRoute(
    name: "Topic", 
    url: "html/{fileName}.html", 
    defaults: new { controller = "Topic", action = "Index" });

the controller is not being called. I have my web.config defined with:

<remove name="WebServiceHandlerFactory-Integrated" />
<add name="HTML" path="*.html" verb="*" 
    type="System.Web.UI.PageHandlerFactory" 
    resourceType="File" preCondition="integratedMode" />

I suspect that I need to call something else besides the PageHandlerFactory or perhaps the issue is something entirely different.

UPDATE: My dev environment is working with integrated pipeline mode, but I need to check if my production environment will support it.

like image 970
Craig Shoemaker Avatar asked Jul 03 '12 21:07

Craig Shoemaker


People also ask

How do you call a controller method from HTML?

cs to determine the controller and action from the url. The default route is {controller}/{action}/{id} where the first two have defaults of "Home" and "Index", respectively, and the third is optional. You can change this if you want by adding/modifying the route set up.

How open HTML file in MVC?

In this example, I will show you how to load an html page in asp.net MVC view. You can call an html page from your view using Html. Action helper method. In controller action you can return an HTML file by using FilePathResult; it will return the content of the file to the response.

Which action is invoked when we call any controller in MVC?

A controller action returns something called an action result. An action result is what a controller action returns in response to a browser request. The ASP.NET MVC framework supports several types of action results including: ViewResult - Represents HTML and markup.

Which ASP.NET MVC object generates the HTML output?

The HtmlHelper class generates HTML elements. For example, @Html.


2 Answers

If you do this:

routes.RouteExistingFiles = true;

You should find this works - even without the handler addition. In the controller you can load the HTML directly using the HostingEnvironment.VirtualPathProvider's GetFile method and do something with it - or better still just use a normal MVC view that renders the same content as the static file, just with your additions.

Although be aware that this means any files that are potentially caught by any routes will be pushed into the MVC pipeline. This isn't generally a concern, however, if decent separation of routes and physical paths is used.

like image 135
Andras Zoltan Avatar answered Oct 12 '22 22:10

Andras Zoltan


I setup the same situation as you and it worked well for me, so you have the key components in place. Some things to keep in mind for the testing and troubleshooting:

Your web.config does need the build provider for the html extension:

<system.web>
    <compilation>
      <buildProviders>
        <add extension=".html"
             type="System.Web.Compilation.PageBuildProvider" />
      </buildProviders>
    </compilation>
</system.web>

A copy and paste of your handlers works for me, so that looks good.

And a copy and paste of your MapRoute works for me too, although I used the default Home controller in a clean project. So as a double check just confirm that you have a controller called Topic with an ActionResult method called Index().

And make sure that your url is localhost.com:{port}/html/test.html with the /html/ in the path since your rule asks for that.

Another good test is to change your MapRoute to use aspx instead and test an aspx page and see if that works. That will confirm whether or not it's the IIS mappings or if it's the MVC rules. If it works with aspx then the issue is related to the handler, but if it fails with aspx too then it's something with MVC.

Also confirm that you're using IIS Express and not Cassini. Cassini will not handle that correctly, but IIS Express will. You can confirm by right-clicking on your project and you should see a menu option called "Use Visual Studio Development Studio...". That will only exist if you are currently using IIS Express.

like image 32
Scott Forsyth - MVP Avatar answered Oct 12 '22 23:10

Scott Forsyth - MVP