开发者

How to Return static HTML and/or Javascript in OpenRasta

开发者 https://www.devze.com 2023-03-14 17:19 出处:网络
I am using OpenRasta 2.0, and am hosting within a console application. I would like to be able to return some static HTML pages and JavaScript files from this setup, for example:

I am using OpenRasta 2.0, and am hosting within a console application.

I would like to be able to return some static HTML pages and JavaScript files from this setup, for example: /index.html /jquery.js

The files are entirely static, i.e. no Handler or Resource is required.

I have added the appropriate files to the project, and initially tried the following syntax:

ResourceSpace.Has
.ResourcesOfType<object>()
.AtUri("/")
.HandledBy<HtmlHandler>()
.RenderedByAspx("~/Views/IndexView.aspx");

The .aspx file is added to the project under a folder 'Views', and is set the build action to 'Embedded Resource'. This results in a NullReferenceException at runtime when attempting to resolve the virtual path. If I set the build action of the file to 'Compile', then it will not compile, I'm guessing because the console project does not understand ASPX.

I have also tried the following shorthand syntax for this available if referencing the WebForms codec:

ResourceSpace.Has
.TheUri("/jquery.js")
.ForThePage("~/Views/jquery.js");

But this suffers from the same issues as my initial approach, although does remove the need for a dummy Handler. So as far as I can tell, the WebForms codec cannot be used within a console application because the ASPX files cannot be compiled.

I was a开发者_StackOverflow社区ble to return HTML using the Razor codec as this expects the view templates to be embedded. However - I was not able to return a JavaScript file with the appropriate media type using the same technique, and I had to turn my otherwise static files into .cshtml files with a @resource defined.

I can't find any examples online of returning static HTML and/or JavaScript using OpenRasta. I would expect to find a dedicated configuration API for this like the "TheUri" syntax but independent of the WebForms codec.

I could create my own 'EmbeddedFileHandler' to return the content of a static embedded file, but I feel like I'm missing something since this is such a simple use case...


Anything that depends on the asp.net pipeline being initialized (such as aspx webforms pages) cannot compile because the BuildProvider is not there to do it, mostly because webforms is too tightly coupled to the asp.net pipeline.

OR 2 was not really designed to be used as a full web stack outside of asp.net for serving static content, as usually the host environment is better suited at doing it, but that's definitly something we're going to address in 3.0.

What I'd suggest is something along the lines of registering FileInfo as a resource, create a handler that can scan the file system for the files you want, and provide your own codec that either stream the data itself or call the API for the host http listener. It should be about 20 lines of code tops and would make a great blog post. :)

0

精彩评论

暂无评论...
验证码 换一张
取 消