【问题标题】:My controller reponse with HTTP ERROR 404我的控制器响应 HTTP ERROR 404
【发布时间】:2022-01-06 14:47:24
【问题描述】:

当我尝试调用 https://localhost:44328/Products 时,我的产品控制器响应 HTTP ERROR 404

我的产品控制器

public class ProductsController : Controller
{
    private readonly CmsShoppingCartContext _context;

    public ProductsController(CmsShoppingCartContext context)
    {
        this._context = context;
    }

    // GET /products
    public async Task<IActionResult> Index(int p = 1)
    {

        //tag helper
        int pageSize = 6;
        //The skip is to skip 0, 1-1*6 is zero. So it starts with one
        var products = _context.Products.OrderByDescending(x => x.Id)
            .Skip((p - 1) * pageSize)
            .Take(pageSize);

        ViewBag.PageNumber = p;
        ViewBag.PageRange = pageSize;
        ViewBag.TotalPages = (int)Math.Ceiling((decimal)_context.Products.Count() / pageSize);

        return View(await products.ToListAsync());


    }

我的 Index.html(在文件夹 Views/Products 中)

【问题讨论】:

  • 请提供代码,不要图片
  • 请出示 Route.cs 文件
  • 我是 MVC 的新手.. 你的意思是 startup.cs?
  • 不,找到 app_Start 文件夹
  • 我不太确定。我的项目中没有这个文件夹。问题是,localhost:44328/Products 不起作用。但是localhost:44328/Products/Shirt 工作正常。

标签: asp.net model-view-controller


【解决方案1】:

由于您使用的是mapendpoints,请尝试将路由属性添加到端点

[Route("~/Products/{p?}")]
public async Task<IActionResult> Index(int p = 1)

另一种方法是映射控制器,但我不建议这样做,因为它非常令人困惑,并且并不总是按预期工作

[Route("~/Products")]
public class ProductsController : Controller

....

[HttpGet("{p?}")]
public async Task<IActionResult> Index(int p = 1)

【讨论】:

  • 现在我得到 GET localhost:44328/products 500 处理请求时发生未处理的异常。 InvalidOperationException:未指定 authenticationScheme,也未找到 DefaultChallengeScheme。可以使用 AddAuthentication(string defaultScheme) 或 AddAuthentication(Action configureOptions) 设置默认方案。
  • @djxenon 错误 500 与路由无关。当服务器出现问题时,这是一个常见错误。据我可以从错误消息中判断,这是身份验证用户错误。你使用令牌或类似的东西吗?
  • 没有意义.. 因为localhost:44328/products/shirt 工作正常.. 但不是localhost:44328/products 没有你写的我应该添加的路由
  • @djxenon google.ca 也在工作。为什么要比较不同的操作?
猜你喜欢
  • 2021-07-27
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 2013-12-16
  • 2015-11-29
相关资源
最近更新 更多