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# 重定向到某个区域_C#_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# 重定向到某个区域

C# 重定向到某个区域,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我无法重定向到某个区域,出现一条消息“处理请求时发生未处理的异常。InvalidOperationException:没有与提供的值匹配的路由”。没有与提供的值匹配的路由 Startup.cs public async Task<IActionResult> GravarCadastro(CadastroModel novoUsuario) { RedirectToActionResult redirecionar; switch (novoUsuario.perfi

我无法重定向到某个区域,出现一条消息“处理请求时发生未处理的异常。InvalidOperationException:没有与提供的值匹配的路由”。没有与提供的值匹配的路由

Startup.cs

public async Task<IActionResult> GravarCadastro(CadastroModel novoUsuario)
{
    RedirectToActionResult redirecionar;
    switch (novoUsuario.perfil)
    {
        case 1:
            //not Working
            redirecionar = RedirectToAction("Index", "Paciente");                  
            break;
        case 2:
            //not Working
            redirecionar = RedirectToAction("Index", "medico");
            break;
        case 3:
            //not Working
            redirecionar = RedirectToAction("Index", "farmacia");
            break;
        case 4:
            //not Working
            redirecionar = RedirectToAction("Index", "laboratorio");
            break;

        default:
           //not Working
            redirecionar = RedirectToAction("Index", "paciente");
            break;
    }

    return redirecionar;
}
我创建了以下路线:

routes.MapRoute("areaRoute","area:exists}/{controller=Admin}/{action=Index}/{id?}");    
LoginController.cs

public async Task<IActionResult> GravarCadastro(CadastroModel novoUsuario)
{
    RedirectToActionResult redirecionar;
    switch (novoUsuario.perfil)
    {
        case 1:
            //not Working
            redirecionar = RedirectToAction("Index", "Paciente");                  
            break;
        case 2:
            //not Working
            redirecionar = RedirectToAction("Index", "medico");
            break;
        case 3:
            //not Working
            redirecionar = RedirectToAction("Index", "farmacia");
            break;
        case 4:
            //not Working
            redirecionar = RedirectToAction("Index", "laboratorio");
            break;

        default:
           //not Working
            redirecionar = RedirectToAction("Index", "paciente");
            break;
    }

    return redirecionar;
}

需要指定该区域,但不存在将其作为直接参数的RedirectToAction重载。所以你需要做:

redirecionar = RedirectToAction("Index", "Paciente", new { area = "Paciente" });    

旁注:不建议将控制器和区域命名为相同的名称。考虑这个地区的多元结构(PACIETAS)。 我试过这个,这个项目是葡萄牙语的,所以是“Paciente”。。。要有创意。我尝试在控制器中没有属性,但它不起作用