我的页面位于:
当我登录到我的应用程序时,我进入HomeController,然后像这样重定向我:
public IActionResult Index()
{
return RedirectToAction("Index", "Messages", new { area = "Action" });
}
但是,出现此错误:
处理请求时发生未处理的异常。 InvalidOperationException:没有路由与提供的值匹配。 Microsoft.AspNetCore.Mvc.Infrastructure.RedirectToActionResultExecutor.ExecuteAsync(ActionContext 上下文,RedirectToActionResult结果)
它很长,但是我发现它与Startup.cs中的最后await.next()行有关:
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();
});
这也是我在Startup.cs中的路由:
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?}");
});
我在其他应用程序中都有这个确切的Startup.cs,它们运行得很好。不确定这个有什么问题吗?
编辑:
完整例外:
Microsoft.AspNetCore.Mvc.Infrastructure.RedirectToActionResultExecutor.ExecuteAsync(ActionContext 上下文,RedirectToActionResult结果) Microsoft.AspNetCore.Mvc.RedirectToActionResult.ExecuteResultAsync(ActionContext 上下文) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult 结果) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext 上下文) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext(下一个参考状态,参考作用域范围,参考对象状态,参考 bool isCompleted) Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter() Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext 上下文)Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref 状态下一个,ref Scope范围,ref对象状态,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 上下文) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext 上下文) Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext 上下文)SendEx.Startup + <> c +
d.MoveNext()在 启动文件 + 等待next(); Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext 上下文) Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext 上下文)
编辑2:
当我删除redirecttoaction并将其替换为return Page()时;页面加载。这意味着区域重定向不起作用?
答案 0 :(得分:0)
仅使用一条路线。尝试如下:-
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces:new[] {"MVCDemo.Controllers"}
);
答案 1 :(得分:0)
return RedirectToAction("Index", "Messages", new { area = "Action" });
这将具有一条类似于-消息/索引/操作的路线 因此,请检查您想要的内容以及该参数是否与参数对齐。
如果您只需要Mssage /或Index /为此,请创建新的路由配置。