Allow other IHttpHandlers within a MojoPortal site

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
5/1/2011 2:06:45 PM
Gravatar
Total Posts 22

Allow other IHttpHandlers within a MojoPortal site

I'm attempting to set up my MojoPortal site to work with a second framework that also implements IHTTPHandlers. Ideally I'd like to have MojoPortal ignore some routes and let my new handler take over within that same specific route.

For example, running locally in IIS 7.5:

http://localhost/  <- should return Mojoportal default home page

http://localhost/myApp/   <- Anything from this path or deeper should be handled by the other IHTTPHandler

Note: my Ihttphandler does not require file names or extensions (so no myapp.ashx is required, though if this is a solution then I could try to make that work -- it's basically a REST style url handler). 

Is it possible to ignore specific paths so that MojoPortal doesn't redirect to default page default.aspx, and does not attempt to look up the page? 

Any advice would be great, and whenever this is working I'll let everyone know the solution. At the moment I am feeling like I've tried all options, but I'm sure I'm missing something.

 

5/1/2011 2:54:22 PM
Gravatar
Total Posts 18439

Re: Allow other IHttpHandlers within a MojoPortal site

Hi Steve,

What I would try is:

1. Use the configuration for extensionless urls.

2. Create a text file Default.ashx at /myapp/Default.ashx with the contents:

<%@ WebHandler Language="C#"  Class="yournamespace.yourhandlerclass" %>

3. Create a text file at /Setup/Routes/MyRoutes.config with contents like this:

<?xml version="1.0"?>
  <Routes>
    <Route name="MyHandlerRoute" routeUrl="myapp/{*}" virtualPath="~/myapp/Default.ashx">
    </Route>
  </Routes>

Hopefully that will work, I'm just theorizing, you can also see other examples of route configuration in comments in /Setup/Routes/mojoRoutes.config

This is some routing plumbing implemented for future use but not yet used in mojoPortal.

Hope that helps,

Joe

5/1/2011 3:03:01 PM
Gravatar
Total Posts 22

Re: Allow other IHttpHandlers within a MojoPortal site

This is definitely a direction I hadn't considered, experimenting now and if I get it working I'll let you know. Thanks!

5/1/2011 4:18:41 PM
Gravatar
Total Posts 22

Re: Allow other IHttpHandlers within a MojoPortal site

Well so far no luck with the Routes. One odd symptom / problem that keeps happening is that the URL requested keeps redirecting repeatedly, each time with an extra /default.aspx tacked to the end, until the server gives up

So, a request to:

http://localhost/myapp/01/32/143

gets into a redirect loop that ends up failing on something like this:

http://localhost/myapp/01/32/143/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx/default.aspx

 

5/1/2011 4:29:31 PM
Gravatar
Total Posts 18439

Re: Allow other IHttpHandlers within a MojoPortal site

Hi Steve,

If the site is configured for folder based child sites it may not work with this model because the first segment is expected to be the folder representing the site.

Another thing you could try is moving the UrlRoutingModule above the mojoUrlRewriter module in Web.config <system.webServer><modules> so it runs before any re-writing.

if that doesn't work.

If you have extensionless urls enabled, try getting rid of the MyRoutes.config file and just put this in the Web.config in the <system.webServer><handlers> section:

<add name="yourhandler" verb="*" path="myapp/*" type="yournamespace.yourclass, yourassembly" preCondition="integratedMode"/>

you might need to try variations on the path, I'm, not sure if it supports ~/ syntax for the app root.

If running in a virtual directory instead of a root site it may need the directory as part of the path.

Hope it helps,

Joe

 

5/1/2011 8:57:08 PM
Gravatar
Total Posts 22

Re: Allow other IHttpHandlers within a MojoPortal site

Well I still have had no luck unfortunately. My goal was to see if I could implement ServiceStack.net services as a special route within the same site as MojoPortal, so that I can use ServiceStack services for Ajax stuff without browser complaints. I've done this successfully in ASP.NET MVC sites and ServiceStack but my attempts today with MojoPortal did not pan out.

Problem #1: MojoPortal and ServiceStack both use IHttpHandlers to intercept requests. Getting one to step aside in favor of the other seems like it should be possible (and once I had it working a little bit), but two confounding factors seemed to be preventing me from success. One is that both my portal (configured this way) and ServiceStack (out of the box) allow extensionless URLs. 

Problem #2: At one point I had ServiceStack answering GET requests, but when a POST request was sent to the same service via JQuery, the parent page itself would do a full postback, forcing the entire page to reload.

If you or anyone else succeeds where I did not, I'd be thrilled to learn the solution, I'd certainly use it myself!  

 

5/2/2011 6:28:53 AM
Gravatar
Total Posts 18439

Re: Allow other IHttpHandlers within a MojoPortal site

Hi Steve,

I'm absolutely sure it can be done, when I get a chance I'll try it myself, but there is nothing special about IHttpHandlers, all .aspx pages implement that, implementing an HttpHandler in a .ashx file is just a much lighter weight object than using (.aspx) pages. Pages are just HttpHandlers with more bells and whistles. You should be able to use custom HttpHandlers no differently than you can use custom .aspx pages. Neither one is involved in routing of requests.

If there is any conflict it has nothing to do with IHttpHandlers used in mojoPortal. The only thing that could possibly conflict is url rewriting vs url routing which is done from HttpModules not handlers. And I have personally experimented with routing and I can get routes to work and am very confident that I could get a route like you describe working to route requests to an HttpHandler. You can even use MVC if you want to (all the references already exist for it and other developers have been using it), though visually blending MVC pages with the WebForms pages would be challenging, it should be very easy for implementing handlers.

When I get a chance to play with it I will post more details.

Best,

Joe

5/4/2011 9:26:21 AM
Gravatar
Total Posts 18439

Re: Allow other IHttpHandlers within a MojoPortal site

Hi Steve,

From what I've read it is best not to use routing with an HttpHandler, but it is very easy to make it handle all requests for urls that start with a folder name like /myapp.

1. Use the configuration for extensionless urls as previously mentioned.

2. Create a class that implements IHttpHandler

Example I tested with:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace mojoPortal.Web.myapp
{
    /// <summary>
    /// Summary description for MyAppHandler 
    /// </summary>
    public class MyAppHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("You requested " + context.Request.Url.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

3. Add a handler mapping in Web.config in <system.webServer><handlers> like this:

<add name="MyAppHandler" verb="*" path="myapp/*" type="mojoPortal.Web.myapp.MyAppHandler, mojoPortal.Web" preCondition="integratedMode"/>

Note that if you are running in a sub directory you may need the subdirectory as part of the path, I'm testing with a root site running at loclahost bbut if you are running under loclahost/mojoportal or some other sub folder then you may need to make the path like mojoportal/myapp/*

Your type would be your namespace.yourclass, yourassembly, I tested directly in mojoPortal.Web but you would use your own custom project.

I can request any url like http://localhost/myapp/foo or http://localhost/myapp/foo.png or foo.aspx etc, and the handler always handles the request, in my example it just writes the requested url.

Hope that helps,

Joe

You must sign in to post in the forums. This thread is closed to new posts.