C# 在IIS中找不到返回404的MVC控制器上调用Get操作
我有一个ASP.net core 2.0 MVC项目,我正在使用axios调用一些控制器操作,以从存储库中获取数据库项的C# 在IIS中找不到返回404的MVC控制器上调用Get操作,c#,asp.net-core,.net-core,axios,asp.net-core-routing,C#,Asp.net Core,.net Core,Axios,Asp.net Core Routing,我有一个ASP.net core 2.0 MVC项目,我正在使用axios调用一些控制器操作,以从存储库中获取数据库项的JsonResult。当我从VisualStudio运行时,这是可行的,但当我发布并在IIS上托管时,axios调用捕获404 not found错误。URL是正确的。即使我尝试在邮递员的网址,我给了404找不到。URL是控制器/操作。奇怪的是索引操作加载视图,但没有给出404。只有axios调用返回404 仅在从Visual Studio运行时有效 下面是axios调用 ax
JsonResult
。当我从VisualStudio运行时,这是可行的,但当我发布并在IIS上托管时,axios调用捕获404 not found错误。URL是正确的。即使我尝试在邮递员的网址,我给了404找不到。URL是控制器/操作。奇怪的是索引
操作加载视图,但没有给出404。只有axios调用返回404
仅在从Visual Studio运行时有效
下面是axios调用
axios.get("Items/GetUnclaimedItems").then(function (response) {
_this.items = response.data;
_this.isBusy = false;
}).catch(function (err) {
alert("Error getting items. " + err);
});
以及控制器内的方法
[HttpGet]
public JsonResult GetUnclaimedItems()
{
Log.Information("Getting unclaimed items");
var itemsList = new List<GikItemViewModel>();
var itemsViewModel = new GikItemViewModel();
var items = _repository.GetUnassignedItemsAsync();
foreach (var item in items)
{
itemsList.Add(new GikItemViewModel
{
Id = item.Id,
Description = item.Description,
CreateDate = item.CreateDate,
Qty = item.Qty,
Value = item.Value,
Selected = false,
Donation = item.Goods == 1 ? "Goods" : item.Services == 1 ? "Services" : "Facilities"
});
}
return Json(itemsList);
}
演示如何配置路由。这看起来像是一个路由问题。只需使用控制器/操作的标准默认路由。当我从VisualStudio运行时,一切都正常,但在发布之后就不行了。VS是否添加了一些我们不知道的路由?否。在本地托管时,路径可能只是相对的,但在部署时,路径可能位于子路径下。确保调用的路径正确。我已经尝试了上面在
axios
调用中显示的路径,项目/GetUnclaimedItems
项目前面没有“/”
。这条路行得通。我认为这是正确的路径,我使用VS发布,所以我不会改变任何文件夹结构。在开发工具中,路径是/Items/GetUnclaimedItems
,因为/Items/Index
返回并加载视图页面。在中包含可用于重现问题的控制器和索引操作。
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Person}/{action=Index}/{id?}");
});