简体   繁体   中英

How to resolve AJAX.POST response as 301 when I am using URL redirection in web.config?

So, after spending lots of time, I had to ask this question. I am sending a AJAX POST request in ASP.NET

 $.ajax({
    type: "POST",
    url: "Manage.aspx/StUpdate",
    data: JSON.stringify(obj),
    contentType: "application/json; charset=utf-8"
    });

And my Web.config which removes all .aspx extension rules as follows:

<rewrite>
<rules>
            <rule name="RemoveASPX" enabled="true" stopProcessing="true">
                <match url="(.*)\.aspx" />
                <action type="Redirect" url="{R:1}" />
            </rule>

            <rule name="AddASPX" enabled="true">
                <match url=".*" negate="false" />
                <conditions>

                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />

                    <add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
                </conditions>
                <action type="Rewrite" url="{R:0}.aspx" />
            </rule>

I tried to send AJAX POST to URL : Manage/StUpdate, It didn't work.

After I read a lot, I got to know that only way I can allow this AJAX POST method to go on is not doing rewrite for this particular file.

Is my understanding right? Is there any other way other than changing URL rewriting?

If I have to remove this particular file from rewriting, How do I do that?

Problem resolved. If someone get into similar problem, solution is with web.config.

<rewrite>
 <rules>
      <rule name="RemoveASPX" enabled="true" stopProcessing="true">
                    <match url="(.*)\.aspx" />
        <conditions>
             <add input="{REQUEST_URI}" pattern="^/Login.aspx" negate="true" />
        </conditions>

         <action type="Redirect" url="{R:1}" />
       </rule>
       <rule name="AddASPX" enabled="true">
         <match url=".*" negate="false" />
         <conditions>
            <add input="{REQUEST_URI}" pattern="^/Login.aspx" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
         </conditions>
         <action type="Rewrite" url="{R:0}.aspx" />
      </rule>
  </rules>
</rewrite>

Solution is to make sure to exclude the particular URL that you want to avoid in both remove and add ASPX condition. I understood that attribute negate="true" stats to avoid this condition while applying this rule. I understood it by trial and error method. Not sure if its right.

Hope it will help someone in need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM