Castle Monorail in Asp.NET web site? - asp.net

In our system most of the code is in an asp.net (2.0) web site, I discovered Castle Monorail a few month ago and I think it's really easier to use than asp.net / webforms.
Here is what we need :
- Use Castle Monorail
- Our code must be in the website (my chief is a kind of old school web developer so he prefer to have some ".cs" files than one ".dll").
- We have to keep the existing webforms code
So maybe if you have a tutorial or something like that (I found a lot of tutorial about asp.net MVC and castle monorail but I did find any with asp.net 2.0)/
Merci les collegues

So it was pretty simple (15 min top) :
1/ Get the element that you need from web.config :
- config section handler
<section name="monorail" type="Castle.MonoRail.Framework.Configuration.MonoRailSectionHandler, Castle.MonoRail.Framework" />
-Configuration itself
<monorail>
<controllers>
<assembly>App_Code</assembly>
<assembly>Castle.Monorail.ViewComponents</assembly>
</controllers>
<viewEngines viewPathRoot="Views">
<add type="Castle.MonoRail.Framework.Views.NVelocity.NVelocityViewEngine, Castle.MonoRail.Framework.Views.NVelocity" />
</viewEngines>
</monorail>
"App_Code" is the name of the web site assembly.
-http handlers
<add verb="*" path="*.rails" type="Castle.MonoRail.Framework.MonoRailHttpHandlerFactory, Castle.MonoRail.Framework" />
<!--block direct user access to template files-->
<add verb="*" path="*.vm" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.boo" type="System.Web.HttpForbiddenHandler" />
<add verb="*" path="*.st" type="System.Web.HttpForbiddenHandler" />
-http modules
<add name="monorail" type="Castle.MonoRail.Framework.EngineContextModule, Castle.MonoRail.Framework" />
2/ Take the dll that you need, in my case (I don't use activerecord) :
Castle.Components.Binder.dll
Castle.Components.Common.EmailSender.dll
Castle.Components.Common.TemplateEngine.dll
Castle.Components.Common.TemplateEngine.NVelocityTemplateEngine.dll
Castle.Components.Validator.dll
Castle.Core.dll
Castle.MonoRail.Framework.dll
Castle.MonoRail.Framework.Views.NVelocity.dll
Castle.MonoRail.ViewComponents.dll
3/ Add a class in your App_Code folder (for instance TestMonorailController) :
using Castle.MonoRail.Framework;
public class TestMonorailController : SmartDispatcherController
{
public TestMonorailController()
{
}
public void OnePage()
{
PropertyBag["toto"] = "TEST";
}
}
4/ Add a Views folder in the root of your website
5/ Add a TestMonorail folder in the folder you just created
6/ Add a file name "OnePage.vm" in this folder :
$toto
7/ Test your website :
http://localhost:XX/YourWebSite/TestMonorail/OnePage.rails
and you should see
"TEST"
Et voila :) I can edit my production code. Thx Ken

I guess that you could put the controller classes in App_Code and be done with it.
you will need to map a special extension for Monorail urls.
If you do not use SOAP webservices (.asmx) then map this extension to Monorail's HttpHandlerFactory.
An interesting culprit could be that the actual assembly containing the App_code files does not have a nice name (I think), and MonoRail does need to know the assembly from which to locate controller classes.
I'd suggest you play with the hints I've listed above and see where it gets you. Share the exceptions you see and hopefully we'll get to the bottom of it soon

Related

Web.config add namespace not applied at file level

I am referencing a class library in a web application project (both in same solution). Within the web pages of the web application, if I do this:
If MyValidation.CorrectEmailFormat(email) Then ...
...Visual Studio 2013 underlines the method, and suggests I import MyCompany.EmailMethods at the top of the page. If I do the Import, the page compiles and the method works okay.
However, because these methods are used extensively across the application, I don't want to add them at page level every time. So I headed for web.config, and did this:
<pages>
<namespaces>
<add namespace="MyCompany.EmailMethods" />
</namespaces>
</pages>
However, VS is still prompting me to perform the Import at the top of every page, and the method is not recognised in the page without doing this. What I am doing wrong please? I assumed from MSDN and other sources this was the correct way to achieve this.
Web application is ASP.Net web pages (4.6).
The reference must be added to the Imported Namespace as described in the following SO post
add-a-namespace-reference-to-all-web-application-web-pages-in-a-different-project
It must be added in the project properties page at the bottom part titled Imported Namespaces
The <pages> directive applies to ASPX files only.
You need to use the equivalent directive for Razor:
<system.web.webPages.razor>
<pages>
<namespaces>
<add namespace="MyCompany.EmailMethods" />

need advice on referencing my libraries from the WebApp

I have 3 Class Libraries that my WebApp references. One of these libraries has folders to organize the classes in it. When creating classes in these sub folders I kept all namespaces as default.
I always thought that if you add the namespaces to the web.config you do not have to add them in 'using statements' at the top of individual aspx.cs pages. In a VB.NET Website I think it works but not in a C# WebApp. Is there anything additional that I must do or is it not possible?
<namespaces>
<add namespace="Library1" />
<add namespace="Library2" />
<add namespace="Library3.Admin" />
<add namespace="Library3.CRM" />
</namespaces>
Also I never knew that you couldn't use the App_Code folder in a Web Application until now. Where would you recommend creating a BasePage.cs file.

How to add another file extension to Razor ViewEngine, e.g. *.html [duplicate]

I manage a large asp.net site which has previously been converted from static html site to asp.net.
For several reasons (mainly SEO) we decided not to rename all the files to .aspx back when we originally converted the site. This was very easy to do by simply adding the buildProvider and httpHandler to the web.config.
<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
<httpHandlers>
<add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory"/>
</httpHandlers>
Now I am upgrading the site to use Asp.net WebPages with Razor cshtml files. I can rename all the files if necessary, and use url rewriting to make the urls stay the same, however it would be much easier if I could just configure the web.config to tell it to parse .html files as if they were .cshtml.
I have searched around quite a bit, and could not find anything equivalent to the PageHandlerFactory for razor pages. It appears as though it is just an internal mechanism in the .net 4.0 ISAPI handler.
The site is currently running on Windows 2003 server and IIS 6. We will be upgrading to 2008/IIS 7.5 in the near future, but I'd prefer not to wait for that.
Is there any way to get the .html files to be parsed by razor as if they were .cshtml files?
Thank you to SLaks for pointing me in the right direction, but it still took a few hours of digging in the MVC source to figure out the solution.
1 - Need to put RazorBuildProvider in web.config
<buildProviders>
<add extension=".html" type="System.Web.WebPages.Razor.RazorBuildProvider"/>
</buildProviders>
And add System.Web.WebPages.Razor to assemblies if it isn't already there.
<assemblies>
[...]
<add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
2 - Add 2 lines in global.asax Application_Start() method
// Requires reference to System.Web.WebPages.Razor
System.Web.Razor.RazorCodeLanguage.Languages.Add(
"html", new CSharpRazorCodeLanguage());
WebPageHttpHandler.RegisterExtension("html");
Call WebPageHttpHandler.RegisterExtension.
You may also need to register a custom WebPageRazorHostFactory to tell the Razor engine what to do with the file; I'm not sure.
As this actually been resolved for use with VS2012 / .net 4.5.
As using the examples above in a C#5 project I get no luck :(

Can I serve .html files using Razor as if they were .cshtml files without changing the extension of all my pages?

I manage a large asp.net site which has previously been converted from static html site to asp.net.
For several reasons (mainly SEO) we decided not to rename all the files to .aspx back when we originally converted the site. This was very easy to do by simply adding the buildProvider and httpHandler to the web.config.
<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
<httpHandlers>
<add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory"/>
</httpHandlers>
Now I am upgrading the site to use Asp.net WebPages with Razor cshtml files. I can rename all the files if necessary, and use url rewriting to make the urls stay the same, however it would be much easier if I could just configure the web.config to tell it to parse .html files as if they were .cshtml.
I have searched around quite a bit, and could not find anything equivalent to the PageHandlerFactory for razor pages. It appears as though it is just an internal mechanism in the .net 4.0 ISAPI handler.
The site is currently running on Windows 2003 server and IIS 6. We will be upgrading to 2008/IIS 7.5 in the near future, but I'd prefer not to wait for that.
Is there any way to get the .html files to be parsed by razor as if they were .cshtml files?
Thank you to SLaks for pointing me in the right direction, but it still took a few hours of digging in the MVC source to figure out the solution.
1 - Need to put RazorBuildProvider in web.config
<buildProviders>
<add extension=".html" type="System.Web.WebPages.Razor.RazorBuildProvider"/>
</buildProviders>
And add System.Web.WebPages.Razor to assemblies if it isn't already there.
<assemblies>
[...]
<add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
2 - Add 2 lines in global.asax Application_Start() method
// Requires reference to System.Web.WebPages.Razor
System.Web.Razor.RazorCodeLanguage.Languages.Add(
"html", new CSharpRazorCodeLanguage());
WebPageHttpHandler.RegisterExtension("html");
Call WebPageHttpHandler.RegisterExtension.
You may also need to register a custom WebPageRazorHostFactory to tell the Razor engine what to do with the file; I'm not sure.
As this actually been resolved for use with VS2012 / .net 4.5.
As using the examples above in a C#5 project I get no luck :(

Custom HttpHandler never running

Forgive me if this is basic. I've never made one before and can't seem to figure out why it's not working. I wrote a little handler to do some parsing on CSS files. I added this:
<system.web>
<httpHandlers>
<remove verb="*" path="*.css"/>
<add verb="*" path="*.css"
type="MyNameSpace.CssRelativePathHandler,CssRelativePathHandler" />
</httpHandlers>
</system.web>
Nothing ever happens. CSS files get parsed normally. No errors, nothing, the code never runs. What am I missing? Shouldn't this cause the handler to be used when *.css files are served? (I added the "remove" later, since I thought perhaps I needed to do that to override a built-in hander, again, no difference either way).
This is IIS 6. I added the IIS 7 code anyway (after searching for answers) but makes no difference.
<system.webServer>
<handlers>
<add name="CssHandler" verb="*" path="*.css"
type="MyNameSpace.CssRelativePathHandler,CssRelativePathHandler" />
</handlers>
</system.webServer>
You need to configure IIS6 to send requests for .css files to ASP.Net.
Had you been using IIS7, your <system.webServer> element would have done that for you, but IIS6 predates this.
For IIS 6 you need to have to tell it to send *.css files to ASP.NET.
Launch IIS Manager
Right-click on Default Web Site
Click on the Home Directory tab
Under Application Settings click on Configuration...
Add a new association for .css and map it to .NET executable:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll
It sounds like you need to configure IIS to enable ASP.NET to execute the .css extension.
Phil Haack has a walkthrough on doing that (just replace .mvc with .css under the heading "Mapping .mvc to ASP.NET"):
http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx
Or you can set up a wildcard mapping in IIS 6:
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/5c5ae5e0-f4f9-44b0-a743-f4c3a5ff68ec.mspx?mfr=true
I'd recommend going with the first method as doing the wildcard approach will send all requests to ASP.NET - so it has a more overhead.
PS: Further down Phil's post, he also lists "IIS6 Extension-less URL" and also covers the wildcard mapping method.

Resources