0
votes

I have a page located in:

  1. An Area called Action
  2. A Controller called Message
  3. A Method called Index

When I log into my application, I go to the HomeController which then redirects me like this:

public IActionResult Index()
{
    return RedirectToAction("Index", "Messages", new { area = "Action" });
}

However, I get this error:

An unhandled exception occurred while processing the request. InvalidOperationException: No route matches the supplied values. Microsoft.AspNetCore.Mvc.Infrastructure.RedirectToActionResultExecutor.ExecuteAsync(ActionContext context, RedirectToActionResult result)

Its very long but I found that it has something to do with my last await.next() line in my Startup.cs:

app.Use(async (context, next) =>
{
    if (context.Response.StatusCode == 404 && !context.Response.HasStarted)
    {
        string originalPath = context.Request.Path.Value;
        context.Items["originalPath"] = originalPath;
        context.Request.Path = "/Error/404";
        await next();
    }
    if (context.Response.StatusCode == 401 && !context.Response.HasStarted)
    {
        string originalPath = context.Request.Path.Value;
        context.Items["originalPath"] = originalPath;
        context.Request.Path = "/Error/401";
        await next();
    }

    if (context.Response.StatusCode == 302 && !context.Response.HasStarted && context.User.Identity.IsAuthenticated)
    {
        string originalPath = context.Request.Path.Value;
        context.Items["originalPath"] = originalPath;
        context.Request.Path = "/Error/302";
        await next();
    }
    if (context.Response.StatusCode == 400 && !context.Response.HasStarted)
    {
        string originalPath = context.Request.Path.Value;
        context.Items["originalPath"] = originalPath;
        context.Request.Path = "/Error/400";
        await next();
    }
    await next();
});

This is my routing in Startup.cs also:

app.UseMvc(routes =>
{
    routes.MapRoute(
      name: "areas",
      template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
    );
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");

});

I have this exact Startup.cs in other applications and they run just fine. Not sure whats wrong with this one?

EDIT:

The full exception:

Microsoft.AspNetCore.Mvc.Infrastructure.RedirectToActionResultExecutor.ExecuteAsync(ActionContext context, RedirectToActionResult result) Microsoft.AspNetCore.Mvc.RedirectToActionResult.ExecuteResultAsync(ActionContext context) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync() Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) SendEx.Startup+<>c+<b__2_0>d.MoveNext() in Startup.cs + await next(); Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

EDIT 2:

When I removed the redirecttoaction and replaced it with a return Page(); The page loads. That means that the area redirection isn't working?

2
it is a typo for controller Message and in route Messages ?? - Mangesh Auti
Nope, I checked it again. Maybe I'll delete and remake it. - JianYA
I tried another controller and its all the same. Something is wrong with my Startup I guess? - JianYA
here you mention return RedirectToAction("Index", "Messages", new { area = "Action" }); "Messages" and in que description Controller called "Message" extra s is it typo error? - Mangesh Auti
No. The controller is called MessagesController - JianYA

2 Answers

0
votes

Use only one route. Try like below:-

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces:new[] {"MVCDemo.Controllers"}
        );
0
votes
return RedirectToAction("Index", "Messages", new { area = "Action" });

this will have a route like - Messages/Index/Action so please check what you want and whether that is aligned with the parameters or not.

If you need just Mssage/ or just Index/ create new route config for that.