locked
Redirecting the user from a shared view based on the previous view RRS feed

  • Question

  • User-1741097413 posted

    I have this situation, where multiple indexes share the same Edit and Details view (indexes are users with different roles). So I need to redirect the Admin to the right index after posting an Edit or getting data in Details. Edit has submit and back buttons for redirecting, while Details has back button. 

    So, this is what I did, the extension: 

    namespace DL.SO.ViewToView.WebUI.Extensions
    {
        public static class HttpRequestExtension
        {
            public static string GetCurrentRelativeUrl(this HttpRequest request)
            {
                return $"{ request.Path }{ request.QueryString }";
            }
        }
    }

    In Edit button (I'm using one partial view for all the indexes)

    <a asp-controller="User" asp-action="Edit" asp-route-id="@item.Id" asp-route-returnUrl="@Context.Request.GetCurrentRelativeUrl()">Edit</a>
    <a asp-controller="User" asp-action="Details" asp-route-id="@item.Id" asp-route-returnUrl="@Context.Request.GetCurrentRelativeUrl()">Details</a>

    In the view model I have this property:

    public string ReturnUrl { get; set; }
    

    That I recive it as a parameter in Get Edit:

    [HttpGet]
    public async Task<IActionResult> Edit(string id, string returnUrl)

    and finally the Post Edit:

            [HttpPost]
            public async Task<IActionResult> Edit(EditUserViewModel model)
            {
                var user = await _userManager.FindByIdAsync(model.Id);
                if (user == null)
                {
                    return NotFound();
                }
                else
                {
                    user.Name = model.Name;
                    if (Url.IsLocalUrl(model.ReturnUrl))
                    {
                        return Redirect(model.ReturnUrl);
                    }
                }
                return View(model);
            }
    

    The redirecting works fine because I have post action, but in the Details View I tried to do it in the view like this: 

            @if (Model.ReturnUrl == "/User/IndexManager")  
            {
                <a asp-controller="User" asp-action="IndexManager">Back to List</a>
    //show some data } else { <a asp-controller="User" asp-action="Index">Back to List</a> }

    I don't like it this way and I don't think it a good approach, does it matter if its case-sensitive? Is it vulnerable to mistakes?

    Regards,

    Friday, September 11, 2020 1:27 AM

Answers

  • User1312693872 posted

    Hi ,rosaud

    You can change it to

    @if (Model.ReturnUrl.Equals("/uSer/inDexmanAger", StringComparison.InvariantCultureIgnoreCase))

    By adding StringComparison.InvariantCultureIgnoreCase,  we do not need to consider case-sensitive when matching the two strings.

    Details:

     public IActionResult Details()
            {
                var model1 = new EditUserViewModel
                {
                    Id = 1,
                    Name = "hello",
                    ReturnUrl = "/USeR/IndexManager"
                };
                return View(model1);
            }

    Details View:

    @if (Model.ReturnUrl.Equals("/uSer/inDexmanAger", StringComparison.InvariantCultureIgnoreCase))
    {
        <a asp-controller="User" asp-action="IndexManager">Back to List</a>
        <div>Matched</div>
    }
    else
    {
        <a asp-controller="User" asp-action="Index">Back to List</a>
    }

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 11, 2020 7:12 AM