Mvc 核心 2.0 中的 HttpGet returns 404
HttpGet returns 404 in Mvc core 2.0
public class SurveyController : Controller
{
public IActionResult Index()
{
var surveys = new List<int>{1};
return View(Surveys);
}
[HttpGet("conditions")]
public IActionResult GetConditions()
{
List<int> Conditions = new List<int{1};
return View("Conditions",Conditions);
}
}
现在浏览量低于
Views/Survey/Index.cshtml
Views/Survey/Conditions.cshtml
路线 Survey/conditions
正在返回 404
。
有人知道吗?
我的startup.cs是-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
试试这个:
public class SurveyController : Controller
{
public IActionResult Index()
{
var surveys = new List<int>{1};
return View(Surveys);
}
[HttpGet("conditions")]
[Route("Survey/conditions")]
public IActionResult GetConditions()
{
List<int> Conditions = new List<int{1};
return View();
}
}
或更改控制器中函数的名称:
public class SurveyController : Controller
{
public IActionResult Index()
{
var surveys = new List<int>{1};
return View(Surveys);
}
[HttpGet("conditions")]
public IActionResult conditions()
{
List<int> Conditions = new List<int{1};
return View();
}
}
或者在浏览器的浏览器路由中写函数名:
路线 = 'Survey/GetConditions'
你对动作的属性:
[HttpGet("conditions")]
指定您希望 URL 为 http://whatever/conditions
。相反,你应该使用:
[HttpGet("/survey/conditions")]
如果您希望以更好的粒度控制路由,则应改用 Route
属性。例如:
[Route("[controller]")] //Set the prefix for subsequent route attributes
public class SurveyController : Controller
{
[Route("conditions")]
public IActionResult GetConditions()
{
List<int> Conditions = new List<int{1};
return View("Conditions",Conditions);
}
}
public class SurveyController : Controller
{
public IActionResult Index()
{
var surveys = new List<int>{1};
return View(Surveys);
}
[HttpGet("conditions")]
public IActionResult GetConditions()
{
List<int> Conditions = new List<int{1};
return View("Conditions",Conditions);
}
}
现在浏览量低于
Views/Survey/Index.cshtml
Views/Survey/Conditions.cshtml
路线 Survey/conditions
正在返回 404
。
有人知道吗?
我的startup.cs是-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
试试这个:
public class SurveyController : Controller
{
public IActionResult Index()
{
var surveys = new List<int>{1};
return View(Surveys);
}
[HttpGet("conditions")]
[Route("Survey/conditions")]
public IActionResult GetConditions()
{
List<int> Conditions = new List<int{1};
return View();
}
}
或更改控制器中函数的名称:
public class SurveyController : Controller
{
public IActionResult Index()
{
var surveys = new List<int>{1};
return View(Surveys);
}
[HttpGet("conditions")]
public IActionResult conditions()
{
List<int> Conditions = new List<int{1};
return View();
}
}
或者在浏览器的浏览器路由中写函数名:
路线 = 'Survey/GetConditions'
你对动作的属性:
[HttpGet("conditions")]
指定您希望 URL 为 http://whatever/conditions
。相反,你应该使用:
[HttpGet("/survey/conditions")]
如果您希望以更好的粒度控制路由,则应改用 Route
属性。例如:
[Route("[controller]")] //Set the prefix for subsequent route attributes
public class SurveyController : Controller
{
[Route("conditions")]
public IActionResult GetConditions()
{
List<int> Conditions = new List<int{1};
return View("Conditions",Conditions);
}
}