在 C# 中路由 ASP.NETcore
Routing in c# ASP.NETcore
_Layout 中的 Products/List 字符串有问题。 cshtml。当我启动该站点并单击“产品”选项卡时,没有重定向到 sheet 部分,它给出了 _Layout 中指定的地址路径的 404 error.Instead。 cshtml (asp-action= "Products/List"),它输出“Products%2FList”。问题与路由设置有关吗?但是,如果您手动输入完整地址:https://localhost:44332/Products/List,则控制器可以毫无问题地找到它。
布局:
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Products/List">Products</a>
</li>
产品控制器:
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AppForPryaniki.Controllers
{
public class ProductsController : Controller
{
private ProductReader reader;
public ProductsController()
{
reader = new ProductReader();
}
// Products/List
public IActionResult List()
{
List<Product> products = reader.ReadFromFile();
return View(products);
}
// Products/Details/1
public IActionResult Details(int id)
{
List<Product> products = reader.ReadFromFile();
Product product = products.Where(x => x.Id == id).FirstOrDefault();
if (product != null)
{
return View(product);
}
else
{
return NotFound();
}
}
}
}
设置路由:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
家庭控制器
namespace AppForPryaniki.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
Product product = new Product();
product.Id = 1;
product.Name = "Test";
return View(product);
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
public IActionResult Products()
{
return View();
}
public IActionResult Test()
{
return View();
}
}
}
你这里有两个错误:
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Products/List">Products</a>
首先,您想要路由到 ProductsController
上的某个操作,因此 Home
是用于 asp-controller
的错误值。相反,您想使用 Products
.
其次,asp-action
的值也是错误的,因为您想要的操作称为 List
,而不是 Products/List
。正确的值为 List
.
更改后的样子如下:
<a class="nav-link text-dark" asp-area="" asp-controller="Products" asp-action="List">Products</a>
当您检查 https://localhost:44332/Products/List
看它是否有效时,您自己解决了一半。当您能够像那样直接导航到某些内容时,这始终表明您尝试在视图中生成的 link 就是问题所在。
控制器和动作的名称不正确。
asp-controller
应该是 Products
而 asp-action
应该是 List
.
_Layout 中的 Products/List 字符串有问题。 cshtml。当我启动该站点并单击“产品”选项卡时,没有重定向到 sheet 部分,它给出了 _Layout 中指定的地址路径的 404 error.Instead。 cshtml (asp-action= "Products/List"),它输出“Products%2FList”。问题与路由设置有关吗?但是,如果您手动输入完整地址:https://localhost:44332/Products/List,则控制器可以毫无问题地找到它。
布局:
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Products/List">Products</a>
</li>
产品控制器:
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AppForPryaniki.Controllers
{
public class ProductsController : Controller
{
private ProductReader reader;
public ProductsController()
{
reader = new ProductReader();
}
// Products/List
public IActionResult List()
{
List<Product> products = reader.ReadFromFile();
return View(products);
}
// Products/Details/1
public IActionResult Details(int id)
{
List<Product> products = reader.ReadFromFile();
Product product = products.Where(x => x.Id == id).FirstOrDefault();
if (product != null)
{
return View(product);
}
else
{
return NotFound();
}
}
}
}
设置路由:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
家庭控制器
namespace AppForPryaniki.Controllers
{
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
Product product = new Product();
product.Id = 1;
product.Name = "Test";
return View(product);
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
public IActionResult Products()
{
return View();
}
public IActionResult Test()
{
return View();
}
}
}
你这里有两个错误:
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Products/List">Products</a>
首先,您想要路由到 ProductsController
上的某个操作,因此 Home
是用于 asp-controller
的错误值。相反,您想使用 Products
.
其次,asp-action
的值也是错误的,因为您想要的操作称为 List
,而不是 Products/List
。正确的值为 List
.
更改后的样子如下:
<a class="nav-link text-dark" asp-area="" asp-controller="Products" asp-action="List">Products</a>
当您检查 https://localhost:44332/Products/List
看它是否有效时,您自己解决了一半。当您能够像那样直接导航到某些内容时,这始终表明您尝试在视图中生成的 link 就是问题所在。
控制器和动作的名称不正确。
asp-controller
应该是 Products
而 asp-action
应该是 List
.