Redirect to URL in ASP CORE

22,020

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
Share:
22,020
Ridikk12
Author by

Ridikk12

C# 0___0

Updated on September 26, 2020

Comments

  • Ridikk12
    Ridikk12 over 3 years

    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.

  • Ridikk12
    Ridikk12 over 7 years
    Thanks for answer. Unfortunately i don't have Possibility to get a absoule url. I always get this relative one and this url can point to other action and controller so i can't use second solution.
  • dotnetstep
    dotnetstep over 7 years
    Please see my update and also I don't understand "User#". That might create problem.
  • Iman Bahrampour
    Iman Bahrampour over 4 years
    For redirect to external URL this answer help you redirect in asp.net core