In a blazor app, is it possible to get the requested path, if the render mode is set to “server”?

陌路散爱 提交于 2020-08-11 01:56:22

问题


I created a Blazor client app and and within this app I have a number of authorization policies with custom requirements and handlers. One of them checks the ID requested in the URL and checks whether the logged in user can view this resource.

For example, through the client, the user navigates to https://localhost/resource/1f28e41c-bc75-44d6-9eef-d46b66b649c7 which is a resource on my API.

I’m using the following code to see the request path:

var httpContext = _httpContextAccessor.HttpContext;
string requestedPath = httpContext.Request.Path.ToString();

This used to work and requestedPath would indeed contain the value “1f28e41c-bc75-44d6-9eef-d46b66b649c7”

However, in the _Host.cshtml I have changed the render mode from "ServerPrerendered" to "Server". This was due to the fact that the code was executed twice at different places during page invocation.

And since I changed this, the requestedPath value is always "/_blazor".

So I was wondering, in a blazor app, is it possible to get the requested path if the render mode is set to "server"?


回答1:


I created a Blazor client app

No you didn't. Your application is a Blazor Server App (also known as server-side Blazor app).

As your app is WebSocket-connection-based, and not HTTP-based, you cannot and should not try to access the HttpContext object. HttpContext does not exist in SignalR-based application like the one you use (Blazor Server App).

The following code snippet creates a Razor Component named Profile, with a parameter (route value) named ID, which you should pass to your IAuthorizationHandler

Profile.razor

@page "/profile"
@page "/profile/{id}"

 <AuthorizeView Policy="Place here the name of your policy" 
                                                Resource="@ID">
      <NotAuthorized>
        <h2 class="mt-5">You are not authorized to view this page</h2>
      </NotAuthorized>
      <Authorized>
        <div class="container my-profile">
            <h2>My Profile</h2>
            --- Place here all the content you want your user to view ----
        </div>
      </Authorized>
</AuthorizeView>

@code {

   [Parameter]
   public string ID { get; set; }

}

Note to remember: The profile ID is passed to your handler method through the AuthorizeView.Resource attribute

And in the handler method you can do something like this:

 public Task HandleAsync(AuthorizationHandlerContext context)
    {
        if (context == null) return Task.CompletedTask;

        // get the profile id from resource, passed in from the profile page 
        // component
        var resource = context.Resource?.ToString();
        var hasParsed = int.TryParse(resource, out int profileID);
        if (hasParsed)
        {
            // compare the requested profileID to the user's actual claim of 
            // profileID
            var isAuthorized = profileID == context.User.GetProfileIDClaim();
            -- --- I can't code blindly any more-----
        }

    }

Hope this helps..



来源:https://stackoverflow.com/questions/60285229/in-a-blazor-app-is-it-possible-to-get-the-requested-path-if-the-render-mode-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!