views:

3759

answers:

6

Is there a quick and dirty way of using a query passed as follows:

domain.com/mypage.aspx/product/toycar/

I've done it before in PHP, but this needs to be done in page (in this instance).

-- I only have access to the aspx page and code behind, and have to work in asp.net 2 (i wish i was using 3.5)

+1  A: 

You might want to look into the ASP.NET System.Web.Routing namespace, which was added in .NET 3.5 SP1 I believe:

http://blogs.msdn.com/mikeormond/archive/2008/05/14/using-asp-net-routing-independent-of-mvc.aspx

http://msdn.microsoft.com/en-us/library/system.web.routing.aspx

You'd be able to get rid of the .aspx extension too.

stusmith
Thanks, I'd read the msdn link already. Unfortunatly I'm stuck with what I'm using which is .net 2 and only having access to the aspx file.
Chris M
+1  A: 

This would involve making a custom HTTP Handler.

Check this

Gerrie Schenck
+1  A: 

In case you just want to read the path from within your .aspx:

Request.ServerVariables["PATH_INFO"]

To clarify:

he has only access to the aspx (+ codebehind) itself, so he must know how the query is, but it is not in the Request.QueryString because of the format. So the only way then is Request.ServerVariables["PATH_INFO"] (Request.RawUrl)

Henk
Thanks for that, useful; but I expect I'll have to resort to mithering them when we get the isapi rewriting.
Chris M
+2  A: 

quick and dirty:

public class ModuleRewriter : IHttpModule
{
    public void Init(HttpApplication application)
    {
        application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
    }

    private void Application_BeginRequest(Object source, EventArgs e)
    {
        // The url will look like: http://domain.com/mypage.aspx/product/toycar/ 
        // The module will rewrite it to: http://domain.com/mypage.aspx?product=toycar

        HttpApplication application = source as HttpApplication;
        string[] urlInfo = application.Request.RawUrl.ToString().Split('/');
        if (urlInfo.Length > 2)
        {
            string page = urlInfo[urlInfo.Length - 3];
            string action = urlInfo[urlInfo.Length - 2];
            string id = urlInfo[urlInfo.Length - 1];
            if (string.IsNullOrEmpty(page))
            {
                page = "default.aspx";
            }
            application.Server.Transfer(string.Format(
                "~/{0}?{1}={2}", page, action, id));
        }
    }

    public void Dispose()
    {
    }
}

web.config:

<httpModules>
    <add name="ModuleRewriter" type="ModuleRewriter, MyWebApplication"/>
</httpModules>

and a test page:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%= Request["product"] %>    
    </div>
    </form>
</body>
</html>
Darin Dimitrov
Can this be done without the web.config edit?
Chris M
No, you need to register the module somewhere. You can do it programmatically in global.asax (in the Application_Start event) if you don't like web.config.
Darin Dimitrov
Were getting isapi-rewrite, I just wondered if I could achieve the same with IIS/.net as I could with an Apache/PHP setup. Obviously I can, just not as easily. Thanks.
Chris M
+1  A: 

You've got a few options, but all of them require access to the web.config and a change to IIS to map all file extensions to the dotNet ISAPI dll:

  1. Use MVC (like stackoverflow does, notice the urls)
  2. Use asp.net routing (new in 3.5)
  3. Write your own http handler Massive guide from Microsoft here
  4. Use the excellent urlrewriting.net which does just about everything perfectly including getting round some awkward authentication and image path problems.

Personally I used urlrewriting.net with good results.

Since you mention you don't have access to anything but the code behind and the page, the only thing I can think of is actually creating those dirs (if you have access to do that) and using a server.transfer page passing the value to your actual page in the folder above. Messy, but if you can't access the other stuff, your choices are limited.

badbod99
A: 

hi i want to create page login with c# asp.net i do it now when the user is login, i want to get user ID and use it in other page. how can i do it? thank you for your help

hani