Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有HttpGet(…)的控制器给出错误404_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# 带有HttpGet(…)的控制器给出错误404

C# 带有HttpGet(…)的控制器给出错误404,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我很难理解为什么我在使用[HttpGet(“/add brand”)]但没有[HttpGet(“/add brand”)]时会出现错误404错误,它工作正常吗 这是我的控制器: public class BrandController : Controller { public IActionResult Index() { return View(); } [HttpGet("/add-

我很难理解为什么我在使用[HttpGet(“/add brand”)]但没有[HttpGet(“/add brand”)]时会出现错误404错误,它工作正常吗

这是我的控制器:

public class BrandController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [HttpGet("/add-brand")]
        public async Task<IActionResult> AddBrand()
        {
            return View();
        }
    }

Camilo是正确的,您不需要路由字符串中的前导/,控制器暗示了/brand/

public class BrandController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    [HttpGet("add-brand")]
    public async Task<IActionResult> AddBrand()
    {
        return View();
    }
}
公共类BrandController:控制器
{
公共IActionResult索引()
{
返回视图();
}
[HttpGet(“添加品牌”)]
公共异步任务AddBrand()
{
返回视图();
}
}

Camilo是正确的,您不需要路由字符串中的前导/号,控制器暗示了/brand/号

public class BrandController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    [HttpGet("add-brand")]
    public async Task<IActionResult> AddBrand()
    {
        return View();
    }
}
公共类BrandController:控制器
{
公共IActionResult索引()
{
返回视图();
}
[HttpGet(“添加品牌”)]
公共异步任务AddBrand()
{
返回视图();
}
}

当您尝试/brand/addbrand时(不带[HttpGet(“/add brand”)):

MVC使用了路由模板“{controller}/{action}” 它将brand与BrandController匹配,并将addbrand与您的动作名称匹配


我很好奇为什么在[HttpGet(“/add brand”)]中需要“/”。如果您尝试[HttpGet(“add brand”)],它应该可以正常工作。

当您尝试/brand/addbrand(没有[HttpGet(/add brand”))时:

MVC使用了路由模板“{controller}/{action}” 它将brand与BrandController匹配,并将addbrand与您的动作名称匹配

我很好奇为什么在[HttpGet(“/add brand”)]中需要“/”。如果您尝试[HttpGet(“添加品牌”)],它应该可以正常工作

路线是:localhost:5001/品牌/添加品牌

因此,您希望该操作可以在来自BrandController的
/brand
中调用。但属性路由不是这样工作的

默认情况下,ASP.NET Core对控制器使用常规路由。它使用的默认模板是
/{controller}/{action}
。这就是为什么如果不使用基于属性的路由,访问
/brand/addbrand
会起作用

但是,一旦您通过属性(或者是
[route(…)]
或者是方法特定的属性,比如
[HttpGet(…)]
)指定了路由,您基本上禁用了基于约定的路由。因此,该操作的路由将仅由路由属性确定。由于您只有一个
[HttpGet(“/add brand”)]
,因此实际的操作路线是
https://localhost:5001/add-品牌
无需控制器的附加路径段

public class BrandController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    [HttpGet("add-brand")]
    public async Task<IActionResult> AddBrand()
    {
        return View();
    }
}
如果您喜欢使用
[HttpGet(“/{controller}/add brand”)]
,可以将其放回。另一种方法是在控制器上添加一个单独的
[Route(“{controller}”)]
,然后将所有操作路由连接到该控制器。请注意,这将禁用所有路由的常规路由,要求您为每个操作指定它

路线是:localhost:5001/品牌/添加品牌

因此,您希望该操作可以在来自BrandController的
/brand
中调用。但属性路由不是这样工作的

默认情况下,ASP.NET Core对控制器使用常规路由。它使用的默认模板是
/{controller}/{action}
。这就是为什么如果不使用基于属性的路由,访问
/brand/addbrand
会起作用

但是,一旦您通过属性(或者是
[route(…)]
或者是方法特定的属性,比如
[HttpGet(…)]
)指定了路由,您基本上禁用了基于约定的路由。因此,该操作的路由将仅由路由属性确定。由于您只有一个
[HttpGet(“/add brand”)]
,因此实际的操作路线是
https://localhost:5001/add-品牌
无需控制器的附加路径段

public class BrandController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    [HttpGet("add-brand")]
    public async Task<IActionResult> AddBrand()
    {
        return View();
    }
}

如果您喜欢使用
[HttpGet(“/{controller}/add brand”)]
,可以将其放回。另一种方法是在控制器上添加一个单独的
[Route(“{controller}”)]
,然后将所有操作路由连接到该控制器。请注意,这将禁用所有路由的常规路由,要求您为每个操作指定它。

如果使用
/route
,则删除控制器的路由。您想要使用的是
[HttpGet(“添加品牌”)]
。我将[HttpGet(“/add brand”)]替换为[HttpGet(“添加品牌”)],但我仍然收到错误404您呼叫的路线是什么?在启动中如何配置路由?我在编辑中添加了我的startup.cs Configure方法。路由是:如果使用
/route
,则删除控制器的路由。您想要使用的是
[HttpGet(“添加品牌”)]
。我将[HttpGet(“/add brand”)]替换为[HttpGet(“添加品牌”)],但我仍然收到错误404您呼叫的路线是什么?在启动中如何配置路由?我在编辑中添加了我的startup.cs Configure方法。路线是:我用[HttpGet(“/add brand”)]替换了[HttpGet(“/add brand”)],我仍然得到错误404,所以你导航到“”?上面的pokes答案解释得很好我用[HttpGet(“/add brand”)]替换了[HttpGet(“/add brand”)],我仍然得到错误404,所以你导航到“”?上面的pokes答案解释得很好我替换了[HttpGet](“/add brand”)]与[HttpGet(“add brand”)]一起,我仍然收到错误404我将[HttpGet(“/add brand”)]替换为[HttpGet(“add brand”)],我仍然收到错误404