locked
using a url rewrite rule in asp.net on iis 7.5 is causing events not to fire on default pages RRS feed

  • Question

  • User1059346350 posted

    Everything was working fine until I added a url rewrite that trims off the default.aspx from a page request. Once I added that all postbacks on default pages go back to the server, but do not fire the correct event.

    The site is .net 2.0 (.net 4.0 is installed, but this site is not using it) The IIS server is version 7.5

    Url rewrite rule:

    <rule name="Default Document URL Rewrite" stopProcessing="true">
        <match url="(.*?)/?Default\.aspx$" />
        <action type="Redirect" url="{R:1}" />
    </rule>

    Sample markup code:

    <form id="form1" runat="server">
        <asp:Button runat="server" ID="btnPostBack" Text="Post Back"
            OnClick="btnPostBack_Click" />
        <asp:Label runat="server" ID="lblDone" />
    </form>

    Sample code behind:

    protected void btnPostBack_Click(object sender, EventArgs e)
    {
        lblDone.Text = "Postback worked!";
    }

    NOTE: The action attribute of the form is not being rendered blank, so adding a line of code in the page_load to fill it explicitly with the Request.RawUrl, like suggested here:http://ruslany.net/2008/10/aspnet-postbacks-and-url-rewriting, did not work. Turns out it's already the same with or without the rewrite rule in place.

    Neither did adding a Forms ControlAdapter like recommended here: Postback doesn't work with aspx page as Default Document.

    Thank you in advance for your assistance!

    Tuesday, September 17, 2013 12:48 AM

All replies

  • User1508394307 posted

    What happens if you change action type?

    <action type="Rewrite"

    Tuesday, September 17, 2013 2:49 AM
  • User1059346350 posted

    405 - HTTP verb used to access this page is not allowed.

    The page you are looking for cannot be displayed because an invalid method (HTTP verb) was used to attempt access.

    Tuesday, September 17, 2013 4:48 AM
  • User1508394307 posted

    Well, I think the problem here is in the rule that makes kind of loop

    <match url="(.*?)/?Default\.aspx$" />
    <action type="Redirect" url="{R:1}" />

    If you request http://localhost/ it will redirect to http://localhost/ so it means again a new redirect? That makes no sense to me. 

    See some ideas how rewrite could be used http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

    So, if we try get rid of the loop and you change rule to something like

    <match url="(.*?)/?test\.aspx$" />

    you will see that postback will start to work.

    Another example

    <match url="^article/(.*?)$" />
    <action type="Rewrite" url="/default.aspx?{R:1}" />

    and call http://localhost/article/123 should work now too.  

    Tuesday, September 17, 2013 4:07 PM
  • User775770345 posted

    The solution suggested by smirnov will not solve this issue. I have the same problem here. We need to get rid of the string "default.aspx" for SEO purposes. The proposed solution relates to "test.aspx". It doesn't resolve the "default.aspx" issue. I suspect that it will cause the same postback issue as mentioned by OP with "test.aspx" too. The Rewrite Rule takes precedence over postback, redirects the page back to itself after removing the page name and stops processing, thereby causing the page to never execute the code meant for the button click. Lionscub, were you able to find a solution to this?

    Wednesday, October 23, 2013 12:33 PM
  • User1508394307 posted

    There are 2 things "events not to fire on default pages" and "get rid of the string "default.aspx" for SEO purposes".

    Last one is most likely an off topic here even it was also mentioned by OP.

    "default.aspx" in the url on postback appears due to the default asp.net behavior. Load your page for the first time, open View Source and find <form> tag. You will see action="default.aspx". So, to change it simply use 

    form1.Action = "/";

    For more ideas look e.g. here http://stackoverflow.com/questions/2063404/changing-postback-url-to-hide-default-aspx

    This "issue" cannot be solved using rewriting because as you understand the form tag is always there and rewrite will generate same loop as it was originally explained.

    If you still have an issue - please start a new thread.

    Thanks,

    Wednesday, October 23, 2013 3:33 PM
  • User1059346350 posted

    First of all, sorry I haven't come around in a while. I had to abandon this part of the project for a bit because it just wasn't working.

    Alankar007 hit the nail on the head.

    As you can see in my initial post I said "Everything was working fine until I added a url rewrite that trims off the default.aspx". Just like Alankar007 said this is for SEO purposes.

    Unfortunately I haven't found a solution yet, and for now have just disabled the rewrite rule.

    Sunday, October 27, 2013 5:26 AM
  • User1508394307 posted

    I think I provided solution in the above post. 

    Simply set form.Action in the default.aspx or master page (if used)

    protected void Page_Load(object sender, EventArgs e)
    {
       form1.Action = Request.RawUrl;
    }
    Sunday, October 27, 2013 5:32 AM
  • User1059346350 posted

    No, that's the solution from the url I posted, but as I said

    "The action attribute of the form is not being rendered blank, so adding a line of code in the page_load to fill it explicitly with the Request.RawUrl, like suggested here:http://ruslany.net/2008/10/aspnet-postbacks-and-url-rewriting, did not work."

    Sunday, October 27, 2013 6:06 AM
  • User1508394307 posted

    If you use this

    <rule name="Default Document URL Rewrite" stopProcessing="true">
        <match url="(.*?)/?Default\.aspx$" />
        <action type="Redirect" url="{R:1}" />
    </rule>

    together with

    form1.Action = Request.RawUrl;

    then it will not solve anything.

    The problem is in the rewrite rule that must be deleted from the web.config. To get rid of default.aspx from the url, you need to keep only the line with form1.Action = Request.RawUrl;  

    Sunday, October 27, 2013 6:21 AM
  • User1059346350 posted

    What?

    That line simply sets the action attribute of the form, it doesn't change the url and will not trim off the default.aspx.

    Sunday, October 27, 2013 7:02 AM
  • User1508394307 posted

    You can test with a new project that does not use rewrite rules and has no other code except default.aspx.

    - Open your new site as http://localhost (no default.aspx in url)
    - click view source and see that <form action="default.aspx"

    Add a button on the page and click on it. You will be redirected to http://localhost/default.aspx, which is a standard behaviour in ASP.NET2

    Now, try to change action from the code and set it to form1.Action = "/";

    - Open your new site as http://localhost (no default.aspx in url)
    - click view source and see that <form action="/"
    - click on button and you will "stay on same URL" 

    Now, put all your required rewrite rules except the one which was for default.aspx (which makes a loop) and see how it works, most likely you would need to change 

    form1.Action = Request.RawUrl;

    to keep postback working with rewrited links.

    P.S.

    I'm not sure if google submits post-forms and there is a sense to do something "for SEO" on this matter.

    P.P.S.

    The above behaviour was changed in ASP.NET4. It now renders the HTML form element’s action attribute value as an empty string when a request is made to an extensionless URL that has a default document mapped to it. 

    http://www.asp.net/whitepapers/aspnet4/breaking-changes 

    So, maybe you would consider to move to ASP.NET4

    Sunday, October 27, 2013 8:25 AM