ASP.NET Core Url.Action返回空字符串 [英] ASP.NET Core Url.Action return empty string
问题描述
3个小时后,在stackoverflow上浏览后,我没有找到解决问题的方法.所以我怀疑我的项目中有什么特别之处.
After 3 hours browsing on stackoverflow I don't find solution of my problem. So I suspect something in my project is special.
我在Visual Studios 2017上有一个ASP.NET Core(2.0)WebApplications项目.我尝试从Kalender.cshtml文件进行ajax调用:
I have a ASP.NET Core (2.0) WebApplications Project on Visual Studios 2017. I try to make a ajax call from my Kalender.cshtml file:
return $.ajax({
type: 'GET',
url: myurl,
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) { //REQUEST SUCCESS
alert(result);
},
带有myurl:
var myurl = '@Url.Action("EventsRead","Kalender")';
但是我知道 alert(myurl)返回一个空字符串.因此,ajax调用必须失败.我猜网址应该是这样的:
but I recognize that alert(myurl) return an empty string. So the ajax call must fail. I guess the url should something like:
/Kalender/EventsRead
但是,如果我使用此功能,则ajax返回404找不到.
but if I use this, ajax return 404 Not found.
我的Kalender.cshtml.cs动作如下:
My Kalender.cshtml.cs Action looks like:
[HttpGet]
public IActionResult EventsRead()
{
//DATABASE READOUT
var events = DataBase.Events.ToList();
return new JsonResult(events);
}
PageModel的类如下:
And the class of the PageModel looks like:
public class KalenderModel : PageModel
这是自动生成的所有内容.我尝试了大约100种不同的版本,但从未在 EventsRead()
操作中插入断点.
Here is everything auto generated. I try about 100 different version but never get a breakpoint into the EventsRead()
action.
边信息:
在我的Startup.cs中,我取消了AntiforgeryToken:
In my Startup.cs I suppress AntiforgeryToken:
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());
});
在发疯之前,我真的需要一些帮助,感谢您的答复.马丁
I really need some help before I get insane, thankful for any response. Martin
推荐答案
对解决方案感兴趣的每个人:
Everybody who interested into the solution:
我在Startup.cs中进行了更改:
I changed in Startup.cs:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//ROUTES DEFAULT SETTINGS
app.UseMvc();
}
收件人:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//ROUTES CUSTOM SETTINGS
app.UseMvcWithDefaultRoute();
}
在此之后
'@Url.Action("TestGet","Kalender")'
将返回正确的路径.
现在我上床哭泣.
这篇关于ASP.NET Core Url.Action返回空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!