开发者

PageMethod with Custom Handler

开发者 https://www.devze.com 2022-12-23 08:01 出处:网络
I have a page based web method which works fine, using [WebMethod], i.e [WebMethod] public static void DoSomethingOnTheServer()

I have a page based web method which works fine, using [WebMethod], i.e

[WebMethod]
public static void DoSomethingOnTheServer()
{
}

I also have a custom page handler to make SEO friendly URLs possible,

e.g

http://localhost/MySite/A_nice_url.ext => http://localhost/MySite/page.aspx?id=1

I am using Server.Transfer in my ProcessRequest handler to achieve this. This all works fine. Also, my page method works fine when the user requests开发者_StackOverflow the URL such as:

http://localhost/MySite/page.aspx?id=1

However, when the user requests the custom handled URL, i.e.

http://localhost/MySite/A_nice_url.ext

The client reports that the PageMethod has been successfully completed, but it has not been called at all.

I'm guessing it has something to do with my custom handler, so I have modified it to include PathInfo:

public void ProcessRequest(HttpContext context) 
{
   // ...  (code to determine id) ...

   // Transfer to the required page
   string actualPath = 
              "~/page.aspx" + context.Request.PathInfo + "?id=" + determinedId;
   context.Server.Transfer(actualPath);
}

So now if a PageMethod is called, it will result in:

http://localhost/MySite/page.aspx/DoSomethingOnTheServer?id=1

I'm wondering if this is the correct syntax for calling a PageMethod.

When I try Server.Transfer reports:

Error executing child request for /MySite/page.aspx/DoSomethingOnTheServer

I've experimented with HttpContext.RewritePath but not sure how to actually make it perform the request.

This does not seem to work, any ideas?


I think that you must use the rewrite url method and avoid the way you going now.

Why not try the

http://urlrewriter.net/

and start learn from this idea, and maybe change your into something like that.

The Server.Transfer is not the way to make url friendly, and in my opinion you must avoid it, and use it only on very special cases.

The Server.Transfer, actually stop the execution of the page some one are, and start execute some other page. This is the code that called. Actually I think that this is not a good way for running every page. And I am not so sure what else happened on some difficult situations, what really happens for example on ResponceWrite, on MSAjax, on control changes, on mutlithreaded pages, etc... and why to make 2 call on every page ? You make the program initialize and run 2 pages every time and not only one. The first is called, to canceled, and then the second is runned. Also from what I see here, they not allow the CallBack.

Use the transfer, only for some special events and not for every call use.

public void Transfer(string path, bool preserveForm)
{
    Page handler = this._context.Handler as Page;
    if ((handler != null) && handler.IsCallback)
    {
        throw new ApplicationException(SR.GetString("Transfer_not_allowed_in_callback"));
    }
    this.Execute(path, null, preserveForm);
    this._context.Response.End();
}

For example - I have use the tranfer, this way: I have a website with multiple names, and I check the webname on the url. If some not type the corrected url, then I am not send them on the normal page, but first I make a server transfer (to keep the same name on url/path) to inform him for that error - then if he like is clicks go to the correct url.

But the page with the message is very very simple.

So I use the tranfer, only for some kind of error, that I like to keep the same url/path, and inform him for that error.

For all the rest, I use the path rewrite, that is made for that reason.

Url Rewrite is used for post also

See this page http://www.mytail.gr/en/Skilakia/Jack_Russell_Terrier.aspx

and then look on the html page source code, and view what I call on form ...

You see that I call the same page again. First think here is that I have change the form url also !, and second is that my program hold the post that make on url rewrite.

Example of my Transfer

See this 2 pages.

http://www.mytail.gr/

http://www.koutavia.gr/

The second is server transfer, and happened on the same code with the first. The second url, is used only on rare cases, and I do not won to lose that user, nether show the same site.

Hope this help you.


In the end, changing the PageMethod to an ASMX ScriptService was the easiest solution which works in conjunction with my existing custom handler.

0

精彩评论

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

关注公众号