Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to URL in ASP CORE

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

My Redirect Action look like:

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

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

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

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

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

My routes:

    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?}");
        });
    }

Thanks for help.

like image 458
Ridikk12 Avatar asked Feb 07 '23 01:02

Ridikk12


1 Answers

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. 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.

  2. 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;  
    

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.

Update 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
like image 161
dotnetstep Avatar answered Feb 08 '23 15:02

dotnetstep