Вопрос

I think I might be having a bit of trouble with my MVC routing. Note, I'm using ASP.NET MVC 4 with Razor Views.

I have my routes registered as follows:

    routes.MapRoute(
        "Person",
        "Person/Show/{uniqueId}",
        new { controller = "Person", action = "Show", uniqueId = "" }
        );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
        );

My PersonController is implemented as follows:

[HandleError]
public class PersonController{
    public ActionResult Show(string uniqueId)
    {
        //get data from database
        var personData = GetPersonDataFromDatabase(uniqueId);

        return View("PersonView", new PersonViewModel(personData));       
    }
}

This is supposed to display the PersonView.cshtml which has a layout of _LayoutContent.cshtml which in turn has a layout of _Layout.cshtml.

Unfortunately, I'm unable to see the page.... unless I'm logged in. And I have no idea why that makes a difference...

When I try to load the page when I'm not logged in I am sent to this page:

http://mymachine:8083/?ReturnUrl=%2fPerson%2fShow%2fvXDwucay

When I look at what is happening using Fiddler I can see that the following happens:

  1. 302, HTTP, mymachine:8083, /Person/Show/vXDwucay
  2. 200, HTTP, mymachine:8038, /?ReturnUrl=%2fPerson%2fShow%2fvXDwucay

For the HTTP 302, I can see that it is returning the following:

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/?ReturnUrl=%2fPerson%2fShow%2fvXDwucay">here</a>.</h2>
</body></html>

Can someone please point me in the right direction of what might be causing this issue? I find it strange that being logged in causes the route to work. I'm sure I must be doing something really simple wrong...or I'm not looking in the right place for the problem.

Это было полезно?

Решение

I finally found the solution to my issue. I was able to track my problem down to a specific revision...and after a lot of staring at the code was finally able to have an epiphany about the solution.

I'm posting solution here just in case someone else has this problem in the future.

In the "PersonView" I was displaying a partial view that was returning some data as follows:

@Html.Action("SomeListPartial", "Other")

When I was reading that, in my mind I had mistakenly thought it was the @Html.ActionLink method which just provides a hyperlink to a page......the @Html.Action method instead "Invokes the specified child action method using the specified controller name and returns the results as an HTML string." ...which means it executes the action and gets back the resulting html for display on the screen.

When I looked at the action being called, it turns out that I had (correctly) placed an [Authorized] attribute on it...which is why it failing when not logged in.

Now I'm not entirely sure why the error wasn't more visible...but I think that there must be something buried in my website that does a redirect which is why I was automatically being taken to ?ReturnUrl=%2fPerson%2fShow%2fvXDwucay

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top