Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在blazor应用程序中,如果渲染模式设置为“服务器”,是否可以获取请求的路径?_C#_Blazor_Httpcontext - Fatal编程技术网

C# 在blazor应用程序中,如果渲染模式设置为“服务器”,是否可以获取请求的路径?

C# 在blazor应用程序中,如果渲染模式设置为“服务器”,是否可以获取请求的路径?,c#,blazor,httpcontext,C#,Blazor,Httpcontext,我创建了一个Blazor客户端应用程序,在这个应用程序中,我有许多具有自定义需求和处理程序的授权策略。其中一个检查URL中请求的ID,并检查登录用户是否可以查看此资源 例如,通过客户端,用户导航到MyAPI上的资源 我使用以下代码查看请求路径: var httpContext = _httpContextAccessor.HttpContext; string requestedPath = httpContext.Request.Path.ToString(); 用于工作和请求的路径实际上包

我创建了一个Blazor客户端应用程序,在这个应用程序中,我有许多具有自定义需求和处理程序的授权策略。其中一个检查URL中请求的ID,并检查登录用户是否可以查看此资源

例如,通过客户端,用户导航到MyAPI上的资源

我使用以下代码查看请求路径:

var httpContext = _httpContextAccessor.HttpContext;
string requestedPath = httpContext.Request.Path.ToString();
用于工作和请求的路径实际上包含值“1f28e41c-bc75-44d6-9eef-d46b66b649c7”

但是,在_Host.cshtml中,我将渲染模式从ServerPrerendered更改为Server。这是因为在页面调用期间,代码在不同的位置执行了两次

因为我改变了这个,所以requestedPath值总是/_blazor

所以我想知道,在blazor应用程序中,如果渲染模式设置为服务器,是否可以获得请求的路径

我创建了一个Blazor客户端应用程序

不,你没有。您的应用程序是Blazor服务器应用程序,也称为服务器端Blazor应用程序

由于您的应用程序基于WebSocket连接,而不是基于HTTP,因此您不能也不应该尝试访问HttpContext对象。HttpContext在基于信号器的应用程序中不存在,就像您使用Blazor服务器应用程序一样

下面的代码段创建了一个名为Profile的Razor组件,其中包含一个名为ID的参数route值,您应该将其传递给您的IAuthorizationHandler

剃须刀 希望这有帮助

我创建了一个Blazor客户端应用程序

不,你没有。您的应用程序是Blazor服务器应用程序,也称为服务器端Blazor应用程序

由于您的应用程序基于WebSocket连接,而不是基于HTTP,因此您不能也不应该尝试访问HttpContext对象。HttpContext在基于信号器的应用程序中不存在,就像您使用Blazor服务器应用程序一样

下面的代码段创建了一个名为Profile的Razor组件,其中包含一个名为ID的参数route值,您应该将其传递给您的IAuthorizationHandler

剃须刀 希望这有帮助

@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; }
 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-----
        }

    }