小编Rod*_*kan的帖子

如何检查用户是否在.Net Core 2.0的Razor页面中进行了身份验证

我想检查用户是否在Razor页面中的ASP.NET Core 2.0应用程序中登录.以下代码适用于.NET 4.6.1:

@if (!Request.IsAuthenticated)
{
    <p><a href="@Url.Action("Login", "Account")" class="btn btn1-success btn-lg" role="button" area="">Sign In &raquo;</a></p>
}
Run Code Online (Sandbox Code Playgroud)

我怎么能在Core 2.0中做到这一点?

authentication asp.net-mvc razor asp.net-core

34
推荐指数
1
解决办法
2万
查看次数

在ASP.NET Core 2.0中将DataTable转换为IEnumerable <T>

我需要从另一个系统的输入中生成一个来自DataTable的IEnumerable.以下代码适用于ASP.NET 4.6.1.

public static IEnumerable<UserAssignmentDto> StaffAssignmentsUsingStoredProcedure(System.Data.DataTable dataTable)
    {
        var data = dataTable.AsEnumerable().Select(row =>
            new UserAssignmentDto
            {
                Id = ((string)row["AssignmentNumber"]),
                Position = (string) row["EsrPositionTitle"],

            });

        return data;
    }
Run Code Online (Sandbox Code Playgroud)

但是,'DataTable'不再包含ASP.NET Core 2.0中'AsEnumerable'的定义.

什么是最有效的方法来生成我需要的'IEnumerable'?

c# linq asp.net asp.net-core

13
推荐指数
4
解决办法
1万
查看次数

在Web API .Net Core中接受x-www-form-urlencoded

我有一个.Net Core Web API,当我尝试发布一些包含一些json的数据时,它返回415不支持的媒体错误.以下是Chrome调试器中返回内容的一部分:

Request URL:http://localhost:51608/api/trackAllInOne/set
Request Method:POST
Status Code:415 Unsupported Media Type
Accept:text/javascript, text/html, application/xml, text/xml, */*
Content-Type:application/x-www-form-urlencoded

action:finish
currentSco:CSharp-SSLA:__How_It_Works_SCO
data:{"status":"incomplete","score":""}
activityId:13
studentId:1
timestamp:1519864867900
Run Code Online (Sandbox Code Playgroud)

我认为这与我的控制器不接受application/x-www-form-urlencoded数据有关 - 但我不确定.我试过装饰我的控制器,Consumes但似乎没有用.

[HttpPost]
[Route("api/trackAllInOne/set")]
[Consumes("application/x-www-form-urlencoded")]
public IActionResult Post([FromBody] PlayerPackage playerPackage)
{ etc..}
Run Code Online (Sandbox Code Playgroud)

任何帮助非常感谢.

以下代码在.Net 4.6.1中运行良好,我能够捕获并处理上面显示的帖子.

[ResponseType(typeof(PlayerPackage))]
public async Task<IHttpActionResult> PostLearningRecord(PlayerPackage playerPackage)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var id = Convert.ToInt32(playerPackage.ActivityId);
        var learningRecord = await _context.LearningRecords.FindAsync(id);
        if (learningRecord == null)
            return NotFound();
etc...
Run Code Online (Sandbox Code Playgroud)

asp.net-core-webapi asp.net-core-2.0

9
推荐指数
3
解决办法
1万
查看次数

使用属性路由接受 .NET Core 2.0 中 API 的多个参数

我有一个传入的 GET 请求,如下所示:

https://localhost:44329/api/scoInfo?activityId=1&studentId=1&timestamp=1527357844844
Run Code Online (Sandbox Code Playgroud)

我将如何在 ASP.net-core 中创建一个接受此请求的端点。

我尝试了以下不同版本,但没有任何效果:

[HttpGet("{activityId: int}/{studentid: int}/{timestamp: int}")]
[Route("api/ScoInfo/{activityId}/{studentid}/{timestamp}")]
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core asp.net-core-webapi

3
推荐指数
1
解决办法
1万
查看次数

使用Asp.Net Core 2.0中的AllowHtml从数据库生成网页

我有一个应用程序使用Asp.Net 4.61,其中我从数据库字段描述中读取html并使用它来填充我的应用程序中的页面.我通过标记属性描述来做到这一点[AllowHtml].

在我的Asp.Net Core 2.0应用程序中,我收到一个错误,即找不到程序集引用或指令.

我有两个问题 - 希望能够让我更好地回答以下问题:

  1. 是否有我可以搜索的文档/网站,以查看AllowHtml是否在Core 2.0中
  2. 如果我想通过从我的数据库中读取填充网页而不是装饰字段/属性,我应该使用更好/更安全的方法[AllowHtml]吗?

c# asp.net asp.net-core

2
推荐指数
1
解决办法
3648
查看次数

导航到 .Net Core 中另一个文件夹中的页面

我有一个使用 Razor Pages 的 .Net Core 应用程序。在页面下,我有两个顶级文件夹 - Prescriptions 和 Admin。在 Prescriptions 文件夹下,我有一些搭建的 CRUD 页面 - 即创建、索引、编辑和删除。当我输入地址https://localhost:44344/Prescriptions时,程序会转到 Prescriptions 文件夹中的索引页面 - 工作正常。

在 Admin 文件夹下,我有一个名为 Client 的页面,其中有一个锚标记:

<a asp-page="./Prescriptions/Index"Add Prescription</a>
Run Code Online (Sandbox Code Playgroud)

我正在尝试导航到页面https://localhost:44344/Prescriptions

我已经尝试过这个标签的一百个不同版本,包括:

<a asp-page="Prescriptions"Add Prescription</a>
<a asp-page="/Prescriptions"Add Prescription</a>
Run Code Online (Sandbox Code Playgroud)

等等,但我无法让它工作。将 href 与相对路径一起使用效果不佳 - 例如 href="/Prescriptions"。我缺少什么?

navigation razor asp.net-core razor-pages

2
推荐指数
1
解决办法
4703
查看次数