重定向到ASP CORE中的URL [英] Redirect to URL in ASP CORE

查看:88
本文介绍了重定向到ASP CORE中的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在重定向到另一个控件和应用程序中的操作时遇到问题.

I have problem with redirect to another controler and action in application.

我的重定向操作如下:

    public IActionResult Redirect(NotificationItemViewModel model)
    {
        NotificationItemToggleReadCommand command = new NotificationItemToggleReadCommand();
        command.Id = new EntityId(model.Id);
        command.MarkAsRead = true;


        var result = CommandProcessor.Run(command);
        RedirectResult redirectResult = new RedirectResult(model.Subject.Url,true);
        return redirectResult;
    }

例如,我的model.Subject.Url格式为:Identity/User#/Details/b868b08c-b3ba-4f45-a7b6-deb02440d42f

My model.Subject.Url is for example in this format : Identity/User#/Details/b868b08c-b3ba-4f45-a7b6-deb02440d42f

是区域/控制器/动作/ID. 问题是我的RedirectResult将我重定向到:

It's area/controller/action/id. Problem is that my RedirectResult redirect me to:

notifications/Identity/User#/Details/b868b08c-b3ba-4f45-a7b6-deb02440d42f 

并且该路由不存在(控制器/区域/控制器/操作/ID) 如何强制这种重定向切断第一个控制器名称?

and that route doesn't exist (controller/area/controller/action/id) How can force this redirect to cut off the first this controller name?

我的路线:

    app.UseMvc(routes =>
        {
            routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action=Index}/{id?}");

            routes.MapRoute(
                name: "controllerActionRoute",
                template: "{controller}/{action}",
                defaults: new { controller = "Home", action = "Index" },
                constraints: null,
                dataTokens: new { NameSpace = "default" });

            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

感谢您的帮助.

推荐答案

如果我正确理解了您的问题,那是因为您在通知区域(其他区域)上,并尝试移出该区域.

If I understand your problem correctly then it is because you are on Notification area ( Some other area) and try to move out of that area.

为此,您可以执行以下任一操作.

For that you can do either of following thing.

  1. 请提供RedirectResult中的绝对URL,而不是提供相对URL.现在的问题是,当应用程序重新启动时,它会考虑相对URL,这就是问题.

  1. Instead of providing relative url , please provide absolute url in RedirectResult. Right now problem is that when application fire redirect it consider relative url and that is problem.

第二种解决方法是使用RedirectToActionResult而不是RedirectResult.

Second solution is to use RedirectToActionResult instead of RedirectResult.

 RedirectToActionResult redirectResult = new RedirectToActionResult("Details", "User", new { @area = "Identity", @Id = "b868b08c-b3ba-4f45-a7b6-deb02440d42f" });
 return redirectResult;  

在上面,我假设区域名称是"Identity",控制器名称是"User",操作是"Details". Acition有一个名为"Id"的参数.您可以根据需要进行更改.在这种情况下,您不必构造绝对URL.

In above I assume that area name is "Identity" and Controller name is "User" and action is "Details". Acition has one parameter that is with name "Id". You can change as per your requirement. In this you don't have to construct absolute url.

更新1

如果我假设您一开始总是必须扎根并且忽略通知".

If I assume that you always have to do root and ignore "notifications" at begining.

至少可以提供这样的网址.

Atleast you can provide Url like this.

~/Identity/User#/Details/b868b08c-b3ba-4f45-a7b6-deb02440d42f

这篇关于重定向到ASP CORE中的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆