views:

92

answers:

2

Hi,

I've got webform routing setup on my asp.net webforms 3.5sp1 project. I would like to have the files for the site in a directory called content including the home page as I would like to run multiple sites using the same system.

In MVC there is a blank default page and the home page is in a folder called home. I can't seem to replicate this behaviour using web form routing but would like to. The blank page is always hit first. the route handler is hit second - it recognises that the request is for the home page and sets up the routing page but is not used. the route handler code is simple:

    public string VirtualPath { get; private set; }

    public IHttpHandler GetHttpHandler(RequestContext
          requestContext)
    {
        string file = requestContext.RouteData.GetRequiredString("File");
        string id = requestContext.RouteData.GetRequiredString("Id");
        string queryString = "?menuid=" + id;
        VirtualPath = "~/" + file;
        HttpContext.Current.RewritePath(
          string.Concat(
          VirtualPath,
          queryString));

        var page = BuildManager.CreateInstanceFromVirtualPath
             (VirtualPath, typeof(Page)) as IHttpHandler;
        return page;
    }

Is there anyway I can do this?

Update Here is my global.asax route code:

    public static void RegisterRoutes(RouteCollection routes)
    {
        Domain.RepositoryFactory repo = new RepositoryFactory();
        foreach (var x in repo.MenuRepository.GetAllEnabledGetMenus())
        {
            if (string.IsNullOrEmpty(x.Url))
            {
                //add default
                System.Web.Routing.RouteTable.Routes.Add(
                    new Route("Default.aspx", 
                        new RouteValueDictionary(new { File = x.FileName, 
                                                      Id = x.Id.ToString() }),
                              new CoreRouteHandler()));
            }
            else
            {
                string url = x.Url;
                if(x.Url.StartsWith("/"))
                {
                    url = url.Remove(0, 1);
                }
                System.Web.Routing.RouteTable.Routes.Add(
                    new System.Web.Routing.Route(url,
                        new RouteValueDictionary(new {File = x.FileName,
                                                      Id = x.Id.ToString()}),
                          new CoreRouteHandler()));
            }

        }

    }
A: 

In my project I was needed to redirect all calls like www.site.com/MyPage to /Pages/MyPage.aspx. Was done by using HttpModule. Sample code below:

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        HttpApplication app = sender as HttpApplication;

        if (app.Context == null || app.Context.Response == null)
            return;

        String sourceUrl = String.Empty;
        try
        {
            sourceUrl = app.Request.FilePath.TrimStart('/').TrimEnd('/').ToLower();
            if (global::MyProject.Global.UrlShortcuts.ContainsKey(sourceUrl))
            {
                String newUrl = global::MyProject.Global.UrlShortcuts[sourceUrl];
                app.Context.RewritePath(newUrl, string.Empty, app.Request.QueryString.ToString(), false);
                                }
            else
            {
            }
        }
        catch (Exception Ex)
        {
            // handle your exception here
        }
    }

The minor issue was with hoster's IIS as I was unable to configure it to process all requests using ASP.NET. So I had to come up with blank placeholder .aspx files for pages (e.g. www.site.com/MyPage/default.aspx) created dynamically at application startup if it's running under IIS.

String server = Context.Request.ServerVariables["SERVER_SOFTWARE"]; 
// IIS puts some stuff here, WebDev server leaves the field empty
Vitaly
thanks for your answer, I could have done it like this but it doesn't use webform routing which I wanted to use.
Charlie Bear
A: 

ok I got it working bit of a doh! moment.

I found this link while googling:

http://blog.ysatech.com/post/2010/07/11/ASP-NET-4-URL-Routing-Default-Page.aspx

all I needed to do was leave the route blank and delete the default.aspx file from the root of the site.

I also copied in the default system.webserver bis from a new MVC project as i think what i had there wasn't right. I'm upgrading an old project and so think it wasn't configured 100%. specifically: was missing.

Charlie Bear