ASP MVC3 - 后HttpPost行动未发现发布 [英] ASP MVC3 - HttpPost action not found after publish
问题描述
我有有几个HttpPost的ActionResult方法的ASP应用MVC3。调试会话期间,该方法很好地工作。但是,当我发布和浏览Web应用程序在浏览器中只有HttpPost方法失败。萤火虫显示404未找到。其他非HttpPost方法相同的控制器做工精细。
更新:
1),该应用的发布网址为localhost / PSP。
2)所有的GET操作返回的HTTP URL://本地主机/ PSP / '控制'/'行动< BR>
3)邮政行动正在返回 HTTP访问URL://本地主机/ '控制'/'行动。
因此,这似乎是一个路由问题。我在我的Global.asax.cs文件的唯一途径是:
routes.MapRoute(
默认,//路线名称
{控制器} / {行动} / {ID},// URL带参数
新{控制器=家,行动=索引,ID = UrlParameter.Optional}
);
请注意,该Home控制器不包含任何HttpPost行动。失败的所有POST方法在单独的控制器。我试图调整这条路线为: PSP / {控制器} / {行动} / {ID}
但随后收到一个错误403.14(仅供参考我使用IIS 7.5)
我被困惑,为什么在调试过程,但不是在已发布的应用程序,这些HttpPost方法的工作。这里是被公布后得到404错误的方法之一:
[HttpPost]
公众的ActionResult GetAreaSelTexResult(JSON_MapSelPars标准杆)
{ AreaSelTextResult myResult =新AreaSelTextResult(); 使用(VAR CTX =新prismEntities())
{
变种Q =从ctx.pPSPMapSummary_Sel P(pars.areaType,pars.areaName,NULL,NULL,pars.goalCDL)的选择P; //这里应该只有一个返回的记录,所以循环将遍历一次。
的foreach(在Q变种K)
{
//移动过程的结果对象
myResult.TopProjectName1 = k.TopProjectName1;
myResult.TopProjectName2 = k.TopProjectName2;
myResult.TopProjectName3 = k.TopProjectName3; myResult.TopProjectSnapshotLink1 = k.TopProjectSnapshotLink1;
myResult.TopProjectSnapshotLink2 = k.TopProjectSnapshotLink2;
myResult.TopProjectSnapshotLink3 = k.TopProjectSnapshotLink3; myResult.TotalProjectAmt =的String.Format({0:C},k.TotalProjectAmt);
myResult.TotalProjectCount = k.TotalProjectCount; }
} //发送对象的局部视图
返回PartialView(GetAreaSelTexResult,myResult);
}
下面是局部视图:
@model PAA.Models.AreaSelTextResult
&LT; P&GT; @ Model.TotalProjectCount项目&LT; / P&GT;
&所述p为H.; @ Model.TotalProjectAmt总&下; / P&GT;&所述p为H.;
&所述; A HREF =@ Model.TopProjectSnapshotLink1&GT; @ Model.TopProjectName1&下; / A&GT;&下; / P&GT;
&所述p为H.;
&所述; A HREF =@ Model.TopProjectSnapshotLink2&GT; @ Model.TopProjectName2&下; / A&GT;&下; / P&GT;
&所述p为H.;
&所述; A HREF =@ Model.TopProjectSnapshotLink3&GT; @ Model.TopProjectName3&下; / A&GT;&下; / P&GT;
这是调用HttpPost方法,然后加载返回的局部视图到一个名为returnedProjDataDIV的JavaScript:
函数retrieveSelectionSummary(selectionName,selectionType){ $('#goalList输入:检查')每个(函数(){。
目标+ = $(本).attr('值')+,;
}); VAR ATTR = {AREANAME:selectionName,
areaType:selectionType,
yearCDL:空,
goalCDL:空,
statusCDL:状态
}; VAR JSON = JSON.stringify(attr)使用;
$阿贾克斯({
网址:'/汇总/ GetAreaSelTexResult',
输入:POST,
数据类型:HTML,
数据:JSON,
的contentType:应用/ JSON的;字符集= UTF-8,
成功:函数(结果){
$('#returnedProjData')HTML(结果);
},
错误:函数(){
警报(错误);
}
});
返回ATTR;
}
决不要硬code的网址是这样的:
网址:'/汇总/ GetAreaSelTexResult',
生成URL时,必须使用网址助手:
网址:'@ Url.Action(GetAreaSelTexResult,摘要),
当你在一个虚拟目录中部署它的code不工作的原因是因为URL /汇总/ GetAreaSelTexResult
不再正确。现在,您必须考虑到虚拟目录名: / myappname /汇总/ GetAreaSelTexResult
。出于这个原因,你应该永远难以code你的URL,但总是用URL助手生成它们。
如果这是在一个单独的JavaScript文件,你不能使用服务器端的助手,你可以定义您的视图一个全局变量,该变量指向正确的URL或使用HTML5数据 - *助手您正在使用的一些DOM元素用。
I have an ASP MVC3 application that has several HttpPost ActionResult methods. During a debug session, the methods work fine. But when I publish and view the web app in the browser only the HttpPost methods fail. Firebug shows a "404 not found". Other non-HttpPost methods in the same controller work fine.
UPDATE:
1) The published url for the app is localhost/psp.
2) All the GET actions return URLs of http://localhost/psp/'controller'/'action'.
3) The POST actions are returning URLs of http://localhost/'controller'/'action'.
So this seems to be a routing problem. The only route I have in my Global.asax.cs file is:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Note that the Home controller does NOT contain any of the HttpPost actions. All POST methods that fail are in separate controller. I've tried to adjust this route to: psp/{controller}/{action}/{id}
but then receive a 403.14 error (FYI I am using IIS 7.5).
I'm baffled by why these HttpPost method work during debug but not in a published app. Here is one of the methods that get the 404 error after being published:
[HttpPost]
public ActionResult GetAreaSelTexResult(JSON_MapSelPars pars)
{
AreaSelTextResult myResult = new AreaSelTextResult();
using (var ctx = new prismEntities())
{
var q = from p in ctx.pPSPMapSummary_Sel(pars.areaType, pars.areaName, null, null,pars.goalCDL) select p;
// There should only be one record returned, so loop will iterate only once.
foreach (var k in q)
{
//Move procedure results to object
myResult.TopProjectName1 = k.TopProjectName1;
myResult.TopProjectName2 = k.TopProjectName2;
myResult.TopProjectName3 = k.TopProjectName3;
myResult.TopProjectSnapshotLink1 = k.TopProjectSnapshotLink1;
myResult.TopProjectSnapshotLink2 = k.TopProjectSnapshotLink2;
myResult.TopProjectSnapshotLink3 = k.TopProjectSnapshotLink3;
myResult.TotalProjectAmt = string.Format("{0:C}", k.TotalProjectAmt);
myResult.TotalProjectCount = k.TotalProjectCount;
}
}
// send object to partial view
return PartialView("GetAreaSelTexResult", myResult);
}
Here is the Partial View:
@model PAA.Models.AreaSelTextResult
<p>@Model.TotalProjectCount projects</p>
<p>@Model.TotalProjectAmt total</p>
<p>
<a href="@Model.TopProjectSnapshotLink1">@Model.TopProjectName1</a></p>
<p>
<a href="@Model.TopProjectSnapshotLink2">@Model.TopProjectName2</a></p>
<p>
<a href="@Model.TopProjectSnapshotLink3">@Model.TopProjectName3</a></p>
And here is the javascript that calls the HttpPost method and then loads the returned partial view into a div called "returnedProjData":
function retrieveSelectionSummary(selectionName, selectionType) {
$('#goalList input:checked').each(function () {
goals += $(this).attr('value') + ",";
});
var attr = { areaName: selectionName,
areaType: selectionType,
yearCDL: null,
goalCDL: null,
statusCDL: status
};
var json = JSON.stringify(attr);
$.ajax({
url: '/summary/GetAreaSelTexResult',
type: 'POST',
dataType: 'html',
data: json,
contentType: 'application/json; charset=utf-8',
success: function (result) {
$('#returnedProjData').html(result);
},
error: function () {
alert("Error.");
}
});
return attr;
}
Never hardcode urls like this:
url: '/summary/GetAreaSelTexResult',
Always use url helpers when generating urls:
url: '@Url.Action("GetAreaSelTexResult", "summary")',
The reason your code doesn't work when you deploy it in a virtual directory is because the url /summary/GetAreaSelTexResult
is no longer correct. You must take into account the virtual directory name now: /myappname/summary/GetAreaSelTexResult
. For this reason you should never hardcode your urls but always use url helpers to generate them.
And if this is in a separate javascript file where you cannot use server side helpers you could define a global variable in your view that will point to the correct url or use HTML5 data-* helpers on some DOM element that you are working with.
这篇关于ASP MVC3 - 后HttpPost行动未发现发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!