C# 按钮单击在本地工作,但在生产中不起作用
我遵循了可能重复的问题中的步骤,但没有解决我的错误。 我的整个应用程序在上传到服务器时正常工作,除了一个按钮 该按钮是一个带有ID的简单按钮。当我单击它时,一个函数捕捉到该单击,从用户输入字段中获取相关数据,将其发送给控制器,并将数据保存到数据库中 当我上传到服务器上时,网站上的其他一切都可以完美地工作。然而,当试图查找控制器操作时,这一个按钮抛出404错误 这是按钮:C# 按钮单击在本地工作,但在生产中不起作用,c#,asp.net-mvc,C#,Asp.net Mvc,我遵循了可能重复的问题中的步骤,但没有解决我的错误。 我的整个应用程序在上传到服务器时正常工作,除了一个按钮 该按钮是一个带有ID的简单按钮。当我单击它时,一个函数捕捉到该单击,从用户输入字段中获取相关数据,将其发送给控制器,并将数据保存到数据库中 当我上传到服务器上时,网站上的其他一切都可以完美地工作。然而,当试图查找控制器操作时,这一个按钮抛出404错误 这是按钮: <input type="button" id="btnAdd" value="Add" class="btn btn-
<input type="button" id="btnAdd" value="Add" class="btn btn-primary" />
控制器操作:
[HttpPost]
public JsonResult InsertDesg(Designee designee)
{
designee.CreatedDate = System.DateTime.Now;
designee.ModifiedDate = System.DateTime.Now;
var user = context.Users.SingleOrDefault(u => u.BhCode == User.Identity.Name);
designee.EnteredBy = user.Id;
db.Designees.Add(designee);
db.SaveChanges();
var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "Designees", new { id = designee.LandownerID, permit = designee.PermitNo, year = designee.ExpYear });
return Json(new { Url = redirectUrl, designee });
}
我的本地成绩:
我的服务器结果:
我希望在服务器上发生与在本地系统上相同的事情。有人能想出解决办法吗?我的IIS日志只显示404错误
解决方案
我不知道为什么会修复它,但我将我的函数调用从上面更改为下面,现在它工作正常:
var RootUrl = '@Url.Content("~/")';
//Send the records to server for saving to database.
$.ajax({
type: "POST",
url: RootUrl + "Designees/InsertDesg",
您是否可以尝试将此代码放入您的操作中,并查看其是否有效:
[HttpPost("Designees/InsertDesg", Order = 1)]
public JsonResult InsertDesg([FromBody]Designee designee)
另一个解决方案是使用
url.Action()
创建ajax url链接
这样,您就不必像您提供的解决方案那样为
RootUrl
变量赋值了。您可以尝试在url中提供完整路径而不是此url:“/Designees/InsertDesg”
至http://your_path/Designees/InsertDesg
可能重复@NoumanZakir您链接的帖子无法解决我的问题;我已经看过了。@Carthax或者不带/
`“指定人/InsertDesg”`试试把“./Designees/InsertDesg”或“~/Designees/InsertDesg”或“/Designees/InsertDesg”放进你的URL。这可能会有所帮助,具体取决于服务器的设置方式。
[HttpPost("Designees/InsertDesg", Order = 1)]
public JsonResult InsertDesg([FromBody]Designee designee)
$(function () {
$("#btnAdd").click(function () {
//snipped for brevity
//Send the records to server for saving to database.
$.ajax({
type: "POST",
url: "@Url.Action("InsertDesg", "Designees")",
data: '{FnameD: "' + txtFnameD.val() + '",LnameD: "' + txtLnameD.val() + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
$("#txtLandownerID").val(r.LandownerID);
$("#txtPermitNo").val(r.PermitNo);
$("#txtExpYear").val(r.ExpYear);
//Add the Name value to first cell.
SetValue(row, 2, txtFnameD);
SetValue(row, 3, txtLnameD);
//Add the row to the WebGrid.
webGrid.append(row);
window.location.href = r.Url;
}
});
});