AJAX Webmethod call returns 404 on MVC3

时光总嘲笑我的痴心妄想 提交于 2019-12-24 02:07:52

问题


I've been using EXTJS 4 and loading my stores through an AJAX call to a Webmethod on the codebehind of an .aspx page. This method has worked for all of my projects until I tried porting my EXTJS 4 work into a MVC3 project. My calls are now returning 404.

The key part is that the project (and the EXTJS4 webmethod calls) works on my colleagues' machines - only my machine is affected by this '404' error. Any Webmethod call, be it one of theirs or written by me, returns as 'Resource Not Found'. What's going on?

Some code if it helps: To Load the Store:

Ext.define('pr.store.Store-Items', {
    extend: 'Ext.data.Store',
    model: 'pr.model.Model-Items',
    pageSize: 200,
    groupField: 'groupID',
    autoLoad: { params: { start: 0, limit: 200 } },
    proxy: {
        type: 'ajax',
        //get data from json file for now
        headers: { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Content-Type': 'application/json; charset=utf-8' },
        url: '/Project/Data.ashx/GetData',           
        reader: {
            type: 'json',
            root: 'd.objects',
            totalProperty: 'd.totalCount'
        },
        extraParams: {
            where: Ext.encode(new Array(''))
            , difference: true
            , mode: 0
        }
    }
});

WebMethod

 [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json, UseHttpGet=true)]
 [System.Web.Services.WebMethod]
 public static object GetData(int start, int limit, string[] where, bool difference, int mode)
 {
     //Code
 }

回答1:


Can be a lot of different things. Starting with wrong configuration of IIS. If you put this URL into browser - do you get anything.




回答2:


I additionally ran in to this issue so removed the following from the Web.Config.

<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

Found in system.webServer > handlers.


Additionally we also have these 3 additional rules in the Global.asax.cs file:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");



回答3:


I had this problem with WebMethods on aspx pages in an ASP.NET / MVC hybrid site.

In IIS, my site's Handler Mappings were not in the right order (Handler Mappings -> View Ordered List).

The ExtensionlessUrl handler must be after the PageHandlerFactory-ISAPI-4.0_32bit, PageHandlerFactory-Integrated-4.0, or whichever similar handler your site uses, depending on if you are in Integrated or Classic, 32- or 64-bit, and your .NET version. Make sure you check which file extension each handler deals with.

If the ExtensionlessUrl handler is before these others, it will treat the request url as an MVC route and 404 when it can't find it.




回答4:


I ran into this while migrating a WebForms site to be capable of using MVC. Since I just needed to get past it temporarily and I don't have any MVC stuff in the site currently, I changed the RouteConfig so that the default route's url is "MVC/{controller}/{action}/{id}". This made it so my existing "MyPage.aspx/webmethod" requests would get processed correctly instead of trying to treat "MyPage.aspx" as the name of a Controller.

For a more long term solution (until all the WebForms, or at least all the web methods, are replaced with MVC) I will probably add something like routes.IgnoreRoute("{page}.aspx/{*webmethod}");

Hope that helps! ;)




回答5:


I ran into this same issue and it ended up being that I ASP.net 4.0 wasn't properly registered with IIS.

To fix this, I ran:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -r


来源:https://stackoverflow.com/questions/9683426/ajax-webmethod-call-returns-404-on-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!