rock.ownar
rock.ownar

Reputation: 113

redirecting to an area

I can not redirect to an area, a message appears "An unhandled exception occurred while processing the request. InvalidOperationException: No route matches the supplied values". No route matches the supplied values.

Startup.cs

I created these routes:

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

I'm using these attributes

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

strutured in image

Upvotes: 1

Views: 67

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273179

The area needs to be specified but there is no overload of RedirectToAction that takes it as a direct parameter. So you need to do:

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

Side note: naming a Controller and an Area the same is not advisable. Consider the plural of Paciente (Pacientas ?) for the Area.

Upvotes: 1

Related Questions