【问题标题】:Call method twice in .net core在.net核心中调用方法两次
【发布时间】:2017-06-01 15:00:06
【问题描述】:

我在启动文件中有这个设置:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "register",
        template: "Register/",
        defaults: new { controller = "Account", action = "Register" });
    routes.MapRoute(
        name: "login",
        template: "Login/",
        defaults: new { controller = "Account", action = "Login" });
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

ClientsController 中的这个函数:

[HttpGet]
public IActionResult Edit(long? id)
{
    var model = _clientsService.GetClientViewModel(id);
    return View(model);
}

当我尝试打电话给http://localhost:59824/Clients/Edit/25 此查询调用Edit 函数两次。但在第一次通话id = 5 和第二次通话id = null。 问题出在哪里?

【问题讨论】:

  • 我使用该路由和该控制器在本地创建了一个新应用程序,它被调用了一次。也许您正在从视图中获得重定向?
  • 当我尝试调用 localhost:59824/Clients/Edit?id=25 时,它只调用一次。
  • 出于某种原因,它尝试调用 favicon.ico 查询 imgur.com/a/8I2A5
  • 从路由中移除可以为空的 long。你为什么要在里面放一个空值? Chrome(和其他人)会自动请求网站图标,而您正在指定一个默认路由,该路由将多余地分配和匹配 id=null。

标签: c# asp.net asp.net-core routes


【解决方案1】:

也许您的 id 是 favicon.ico,所以它是 null。从您的项目中查找并删除 favicon。我遇到了这个问题,并通过这种方式解决了它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 2019-12-10
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    相关资源
    最近更新 更多