AJAX Webmethod调用在MVC3上返回404

时间:2012-03-13 11:50:04

标签: ajax asp.net-mvc-3 extjs http-status-code-404

我一直在使用EXTJS 4并通过对.aspx页面代码隐藏的Webmethod的AJAX调用来加载我的商店。这个方法适用于我的所有项目,直到我尝试将EXTJS 4工作移植到MVC3项目中。我的电话现在正在返回404。

关键部分是项目(以及EXTJS4 webmethod调用)在我同事的机器上运行 - 只有我的机器受到'404'错误的影响。任何Webmethod调用,无论是他们的一个或由我编写,都会返回“未找到资源”。发生了什么事?

一些代码如果有帮助: 加载商店:

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
 }

4 个答案:

答案 0 :(得分:6)

我还遇到了这个问题,所以从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" />

在system.webServer&gt;中找到处理程序。


此外,我们在Global.asax.cs文件中还有以下3个附加规则:

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

答案 1 :(得分:3)

我在ASP.NET / MVC混合网站的aspx页面上遇到了WebMethods的这个问题。

在IIS中,我的网站的处理程序映射的顺序不正确(处理程序映射 - &gt;查看有序列表)。

ExtensionlessUrl处理程序必须之后 PageHandlerFactory-ISAPI-4.0_32bit,PageHandlerFactory-Integrated-4.0,或者您网站使用的任何类似处理程序,具体取决于您是使用Integrated还是Classic,32-或64位,以及您的.NET版本。确保检查每个处理程序处理的文件扩展名。

如果ExtensionlessUrl处理程序在这些其他处理程序之前,它会将请求URL视为MVC路由,而在无法找到它时将其视为404.

答案 2 :(得分:0)

我遇到了同样的问题,结果是因为我没有在IIS中正确注册ASP.net 4.0。

为了解决这个问题,我跑了:

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

答案 3 :(得分:0)

我在迁移WebForms站点时遇到了这个问题,以便能够使用MVC。由于我只需要暂时通过它并且我当前没有任何MVC内容,我更改了RouteConfig,以便默认路由的url是“MVC / {controller} / {action} / {id}”。这使我现有的“MyPage.aspx / webmethod”请求得到正确处理,而不是试图将“MyPage.aspx”视为控制器的名称。

对于更长期的解决方案(直到所有WebForms,或者至少所有Web方法都被MVC替换),我可能会添加类似routes.IgnoreRoute("{page}.aspx/{*webmethod}");

的内容

希望有所帮助! ;)