【问题标题】:redirecting to an area重定向到某个区域
【发布时间】:2019-05-17 04:50:39
【问题描述】:

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

Startup.cs

我创建了这些路线:

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;
}

PacienteController.cs

我正在使用这些属性

[AllowAnonymous] 
[Area("Paciente")]
[Route("Paciente")]
public class PacienteController : Controller
{
    //[Authorize]
    public ActionResult Index()
    {
        return View();
    }
}

strutured in image

【问题讨论】:

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


    【解决方案1】:

    需要指定区域,但没有将其作为直接参数的 RedirectToAction 重载。所以你需要这样做:

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

    旁注:不建议将控制器和区域命名为相同。考虑该地区的 Paciente (Pacientas ?) 的复数形式。

    【讨论】:

    • 我试过了,项目是葡萄牙语的,所以是“Paciente”
    • 嗯...要有创意。
    • 我试过没有控制器中的属性,但它不起作用
    猜你喜欢
    • 2018-04-19
    • 2011-03-02
    • 2011-09-19
    • 1970-01-01
    • 2017-01-18
    • 2016-03-21
    • 2011-01-04
    • 1970-01-01
    • 2011-04-20
    相关资源
    最近更新 更多