0

Given an MVC route:

[Route("user/{userId}")]
public async Task<ActionResult> GetUser(int userId)
{
    var vm = await _userService.GetVm();
    return View(vm);
}

Currently the browser displays /user/2 however I want to "redirect" to /, stripping the slug. I say "redirect" because I do not want to reroute; I want to do the work within GetUser and return a response that tells the browser to change the displayed URL accordingly. Is this possible?

maccettura
  • 10,514
  • 3
  • 28
  • 35
BLAZORLOVER
  • 1,971
  • 2
  • 17
  • 27
  • Its not really clear to me what you are asking. You want to do everything in `GetUser()` then when that is done you want your URL to just be `/` ? – maccettura May 30 '18 at 18:12
  • @maccettura yes. `site.com/user/2` > GetUser() > `site.com` – BLAZORLOVER May 30 '18 at 18:13
  • You want to change the url in the response after the work is done? – L0uis May 30 '18 at 18:15
  • I think I understood what you mean, did you thought about attaching javascript to the view that changes the url ? its not very clean but that's what I can think of, you need client side scripting for changing the url "without redirect" – Eden Reich May 30 '18 at 18:15
  • @L0uis yes..... – BLAZORLOVER May 30 '18 at 18:15
  • redirect to another view: https://stackoverflow.com/questions/546461/asp-net-mvc-redirect-to-a-different-view – L0uis May 30 '18 at 18:31

2 Answers2

3

return a response that tells the browser to change the displayed URL accordingly

That's not how HTTP requests/responses work. The client requests a URL, the server doesn't respond with a URL. But it can respond with a variety of responses. For example, if you want the client to issue a new request to a specific URL, that's a redirect:

return Redirect(Url.Content("~/"));

However, if you really want to respond with content:

return View(vm);

Then your content would need to do something which changes the URL. That would be in client-side code. I wouldn't expect that to be 100% reliable, and it wouldn't surprise me if some browsers (or some browser settings) consider it a potential security spoof and don't allow it.


Between these two approaches, the former is the most common. But the operation you're showing in your code doesn't seem to warrant it. If your intent is to show information about a user (or any record in the data) from the site root without requiring that the client navigate to a URL which requests it, perhaps what you want is for your site root to make an AJAX request for the data instead.

David
  • 208,112
  • 36
  • 198
  • 279
0

Redirect to the Home Index action.

If you are within the Home Controller do this:

public ActionResult Index()
{
    return View()
}

[Route("user/{userId}")]
public async Task<ActionResult> GetUser(int userId)
{
    var vm = await _userService.GetVm();

    return PartialView("_JSRedirect", new JSRedirectViewModel() { Location = Url.Action("Index") });
}

Your JSRedirectViewModel will look like this:

public class JSRedirectViewModel
{
    public string Location { get; set; }
}

Your _JSRedirect Partial View will look like this:

@model Adservio.Models.JSRedirectViewModel
@{
    Layout = null;
}

<script type="text/javascript">
    window.location = '@Model.Location';
</script>

You would typically call the GetUser Action from your view this way:

<div id="AlertDiv"></div>
<div id="MsgDiv"></div>

<button onclick="GetUser()">Perform User Action</button>

<script type="text/javascript">

            $.post({
                 url: "@Url.Action("GetUser")",
                 data: {  },
                 global: false,
                 success: function (r) {
                     $('#AlertDiv').html(r);
                }
            });

</script>

Apologies for the JQuery...

Enoch Olaoye
  • 144
  • 1
  • 6