ASP.NET CORE 5 MVC缺少除HOMECONTROLLER以外的所有控制器的默认操作
我使用ASP.NET Core 5 MVC,并且有一个有趣的问题。当我想加载任何控制器的索引
方法时,我也必须将索引动作名称写入URL。如果我只在URL中写入控制器名称,那么它就不会加载。我没有任何例外。我没有在root目录中使用名称与任何控制器名称匹配的文件夹。我使用默认路由,所以我不明白有什么问题。但是我在调试时注意到了一个有趣的东西。我尝试通过数据通知设置自定义路由路径。当我重命名控制器时,我会得到模棱两可的
异常。
“模棱两可的matchException:请求匹配多个端点。 匹配:
CANTEENFEEDBACK2._0.CONTROLLERS.ADMINPANELCONTROLLER.LOGIN (CanteenFeedBack2.0) CANTEENFEEDBACK2._0.CONTROLLERS.ADMINPANELCONTROLLER.EDITQUESTION (CanteenFeedBack2.0) CANTEENFEEDBACK2._0.CONTROLLERS.ADMINPANELCONTROLLER.STATISTIS (CanteenFeedBack2.0) CANTEENFEEDBACK2._0.CONTROLLERS.ADMINPANELCONTROLLER.GETSTATISTIS (CanteenFeedBack2.0) CANTEENFEEDBACK2._0.CONTROLLERS.ADMINPANELCONTROLLER.INDEX (CanteenFeedBack2.0) CANTEENFEEDBACK2._0.CONTROLLERS.ADMINPANELCONTROLLER.AUTHERROR (CanteenFeedback2.0) CANTEENFEEDBACK2._0.CONTROLLERS.HOMECONTROLLER.INDEX (CanteenFeedBack2.0)“
adminpanelController
的详细信息:
public class AdminPanelController : Controller
{
private readonly DatabaseContext DB;
public AdminPanelController(DatabaseContext dB)
{
DB = dB;
}
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IActionResult Index()
{
string userName = User.Claims.Where(claim => claim.Type == ClaimTypes.NameIdentifier).First().Value;
string lastName = User.Claims.Where(claim => claim.Type == ClaimTypes.GivenName).First().Value;
bool exists = false;
string userId = User.Claims.Where(claim => claim.Type == ClaimTypes.NameIdentifier).First().Value;
if (DB.IdmAccounts.FirstOrDefault(i => i.AccountId == userId) != null)
{
exists = true;
}
else
{
exists = false;
}
IEnumerable<Category> categories;
IEnumerable<Subcategory> subcategories;
IEnumerable<Question> questions;
try
{
categories = DB.Categories;
subcategories = DB.Subcategories;
questions = DB.Questions
.Where(q => q.isArchived == false)
.Include(q => q.Categories)
.Include(q => q.Subcategories)
.ThenInclude(sc => sc.Category);
}
catch (SqlException)
{
return Content("Nem sikerült betölteni a kérdéseket");
}
catch (Exception)
{
return Content("Nem sikerült betölteni a kérdéseket");
}
ViewBag.lastName = lastName;
ViewBag.exists = exists;
ViewBag.categories = categories;
ViewBag.subcategories = subcategories;
ViewBag.questions = questions;
return View();
}
[HttpPost]
public IActionResult SessionSave(string token)
{
HttpContext.Session.SetString("JWToken", token);
return Redirect("http://localhost:5000/AdminPanel/Index");
}
[HttpGet]
public IActionResult Login()
{
return View();
}
public IActionResult AuthError()
{
return View();
}
}
以及homecontroller
的详细信息:
public class HomeController : Controller
{
public IActionResult Index()
{
// Cookie options
CookieOptions cookieOptions = new CookieOptions();
cookieOptions.Expires = DateTime.Now.AddDays(365);
// Add default cookie
Response.Cookies.Append("lang_cookie", Languages.HU.ToString(), cookieOptions);
// Setting language to view
Language language = new Language(Pages.Home, Languages.HU);
return View(language.GetPageTexts());
}
[Route("{changedLan}")]
public IActionResult Index(Languages changedLan)
{
// Cookie options
CookieOptions cookieOptions = new CookieOptions();
cookieOptions.Expires = DateTime.Now.AddDays(365);
// Update cookie
Response.Cookies.Append("lang_cookie", changedLan.ToString(), cookieOptions);
Language language = new Language(Pages.Home, changedLan);
return View(language.GetPageTexts());
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
I use ASP.NET Core 5 MVC and I have an interesting issue. When I want load the Index
method of any controller, then I have to write index action name into URL too. If i write only controller name into the URL, then it load nothing. I don't get any exception. I haven't folder in root directory with name what matching with any controller name. I use default routing, so i don't understand what is the problem. But i noticed an interesting stuff while debugging. I tried set a custom route path with data-annotation. When I renamed the controller, then I get an AmbiguousMatch
exception.
"AmbiguousMatchException: The request matched multiple endpoints.
Matches:CanteenFeedback2._0.Controllers.AdminPanelController.Login
(CanteenFeedback2.0)
CanteenFeedback2._0.Controllers.AdminPanelController.EditQuestion
(CanteenFeedback2.0)
CanteenFeedback2._0.Controllers.AdminPanelController.Statistic
(CanteenFeedback2.0)
CanteenFeedback2._0.Controllers.AdminPanelController.GetStatistic
(CanteenFeedback2.0)
CanteenFeedback2._0.Controllers.AdminPanelController.Index
(CanteenFeedback2.0)
CanteenFeedback2._0.Controllers.AdminPanelController.AuthError
(CanteenFeedback2.0)
CanteenFeedback2._0.Controllers.HomeController.Index
(CanteenFeedback2.0)"
A detail from AdminPanelController
:
public class AdminPanelController : Controller
{
private readonly DatabaseContext DB;
public AdminPanelController(DatabaseContext dB)
{
DB = dB;
}
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public IActionResult Index()
{
string userName = User.Claims.Where(claim => claim.Type == ClaimTypes.NameIdentifier).First().Value;
string lastName = User.Claims.Where(claim => claim.Type == ClaimTypes.GivenName).First().Value;
bool exists = false;
string userId = User.Claims.Where(claim => claim.Type == ClaimTypes.NameIdentifier).First().Value;
if (DB.IdmAccounts.FirstOrDefault(i => i.AccountId == userId) != null)
{
exists = true;
}
else
{
exists = false;
}
IEnumerable<Category> categories;
IEnumerable<Subcategory> subcategories;
IEnumerable<Question> questions;
try
{
categories = DB.Categories;
subcategories = DB.Subcategories;
questions = DB.Questions
.Where(q => q.isArchived == false)
.Include(q => q.Categories)
.Include(q => q.Subcategories)
.ThenInclude(sc => sc.Category);
}
catch (SqlException)
{
return Content("Nem sikerült betölteni a kérdéseket");
}
catch (Exception)
{
return Content("Nem sikerült betölteni a kérdéseket");
}
ViewBag.lastName = lastName;
ViewBag.exists = exists;
ViewBag.categories = categories;
ViewBag.subcategories = subcategories;
ViewBag.questions = questions;
return View();
}
[HttpPost]
public IActionResult SessionSave(string token)
{
HttpContext.Session.SetString("JWToken", token);
return Redirect("http://localhost:5000/AdminPanel/Index");
}
[HttpGet]
public IActionResult Login()
{
return View();
}
public IActionResult AuthError()
{
return View();
}
}
And a detail from HomeController
:
public class HomeController : Controller
{
public IActionResult Index()
{
// Cookie options
CookieOptions cookieOptions = new CookieOptions();
cookieOptions.Expires = DateTime.Now.AddDays(365);
// Add default cookie
Response.Cookies.Append("lang_cookie", Languages.HU.ToString(), cookieOptions);
// Setting language to view
Language language = new Language(Pages.Home, Languages.HU);
return View(language.GetPageTexts());
}
[Route("{changedLan}")]
public IActionResult Index(Languages changedLan)
{
// Cookie options
CookieOptions cookieOptions = new CookieOptions();
cookieOptions.Expires = DateTime.Now.AddDays(365);
// Update cookie
Response.Cookies.Append("lang_cookie", changedLan.ToString(), cookieOptions);
Language language = new Language(Pages.Home, changedLan);
return View(language.GetPageTexts());
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论