小编Val*_*tor的帖子

无法让 LinkGenerator 创建 API 操作的路径

我正在尝试从服务内部(控制器外部)创建到 API 端点的链接。

这是控制器及其基类。我在 ASP.NET Core 中使用 API 版本控制和区域。

[ApiController]
[Area("api")]
[Route("[area]/[controller]")]
public abstract class APIControllerBase : ControllerBase
{

}

[ApiVersion("1.0")]
public class WidgetsController : APIControllerBase
{
    [HttpGet("{id}"]
    [Produces("application/json")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<Widget>> Get(Guid id)
    {
        // Action...
    }
}
Run Code Online (Sandbox Code Playgroud)

API 版本控制配置:

services.AddApiVersioning(options =>
{
    options.ApiVersionReader = ApiVersionReader.Combine(
        new QueryStringApiVersionReader
        {
            ParameterNames = { "api-version", "apiVersion" }
        },
        new HeaderApiVersionReader
        {
            HeaderNames = { "api-version", "apiVersion" }
        });
});
Run Code Online (Sandbox Code Playgroud)

我实际上尝试使用 LinkGenerator 的地方:

_linkGenerator.GetPathByAction(
    _accessor.HttpContext,
    action: "Get",
    controller: "Widgets",
    values: …
Run Code Online (Sandbox Code Playgroud)

c# url-routing asp.net-core-mvc asp.net-core

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

仅在EF Core中为"true"创建唯一约束

我有一个跟踪记录附件的课程.每个记录可以有多个RecordAttachments,但是要求每个记录只能有一个RecordAttachment标记为IsPrimary.

public class RecordAttachment
{
    public int Id { get; set; }
    public int RecordId { get; set; }
    public string Details { get; set; }
    public bool IsPrimary { get; set; }

    public Record Record { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我不能只使用.HasIndex(e => new { e.RecordId, e.IsPrimary }).IsUnique(true)因为false每个记录可以有多个值.

基本上,我需要一个唯一约束RecordIdIsPrimary == true,虽然这并不工作:

entity.HasIndex(e => new { e.RecordId, IsPrimary = (e.IsPrimary == true) }).IsUnique(true)

编辑:查看这样的答案:比特列的唯一约束只允许1个真(1)值看起来这可能直接用SQL创建约束,但是它不会反映在我的模型中.

c# sql-server entity-framework-core

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

这段代码是如何在不同的线程中执行的?

在Windows窗体项目中,我有一个按钮处理程序,可以在记事本中打开文件进行编辑.一旦记事本关闭,我调用一个函数RefreshTextBox()来解析文本文件并根据值更新TextBox.以下是打开记事本并在关闭时调用refresh方法的方法:

private void button_Click(object sender, EventArgs e)
    {
            Process p = new Process
            {
                EnableRaisingEvents = true,
                StartInfo =
                {
                    FileName = "NOTEPAD.EXE",
                    Arguments = _path,
                    WindowStyle = ProcessWindowStyle.Maximized,
                    CreateNoWindow = false
                }
            };

            p.Exited += (a, b) =>
            {
                RefreshTextBox();
                p.Dispose();
            };

            p.Start();
    }
Run Code Online (Sandbox Code Playgroud)

和刷新文本框的代码:

private void RefreshTextBox()
    {
        using (StreamReader reader = File.OpenText(_appSettingsPath))
        {
            string text = reader.ReadToEnd();

            // Code to parse text looking for value...

            // InvalidOperationException thrown here:
            textBox.Text = reader.Value.ToString();
        }
    }
Run Code Online (Sandbox Code Playgroud)

这会引发一个异常,试图从一个创建它的线程以外的线程更新Control.我很难理解为什么.我不会在新的任务或背景工作者或类似的事情中这样做.显然,记事本正在另一个线程中运行,但刷新方法直到它的进程退出后才会被调用. …

.net c# multithreading winforms

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

ASP.NET Core Html Helper 使用 TagBuilder 渲染原始文本而不是格式化 HTML

我正在开发一个 Html Helper 来创建一个由多个元素组成的控件,使用TagBuilders 构建。控件本身将使用TagBuilder包含 adiv和所有子元素的 a 进行渲染。

据此: https: //developercommunity.visualstudio.com/content/problem/17287/tagbuilder-tostring-returns-the-type-of-tagbuilder.html

我实现了一种Render()方法来创建控件并将其返回为string

public class MyCustomControl
{
    public override string ToString()
    {
        return Render();
    }

    private string Render()
    {
        TagBuilder mainContainer = new TagBuilder("div");

        // Generate child elements and append to mainContainer...

        using (StringWriter writer = new StringWriter())
        {
            mainContainer.WriteTo(writer, HtmlEncoder.Default);

            return writer.ToString();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并创建了一个扩展方法来在 Razor 视图中调用它:

public static MyCustomControl(this IHtmlHelper html)
{
    return new MyCustomControl();
}
Run Code Online (Sandbox Code Playgroud)

并将其包含在视图中,如下所示:

@(Html.MyCustomControl() …
Run Code Online (Sandbox Code Playgroud)

c# razor asp.net-core

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

Self referencing loop from Newtonsoft JsonSerializer using Entity Framework Core

I've encountered the error:

JsonSerializationException: Self referencing loop detected for property 'Subject' with type 'Project.Models.Subject'. Path 'data[0].Totals'.

It occurs when I load a View with a dataGrid populated by an IEnumerable<Subject> model. The Grid is a DevExtreme DataGrid bound to the View's model like this:

@(Html.DevExtreme().DataGrid()
    .DataSource(Model)
    .Paging(paging =>
    {
        paging.Enabled(true);
        paging.PageIndex(0);
        paging.PageSize(20);
    })
    .Columns(columns =>
    {
        columns.Add().DataField("SubjectId");
        ... other fields
    })
)
Run Code Online (Sandbox Code Playgroud)

Which is populated from a Controller that pulls data from a Repository with this function:

public async …
Run Code Online (Sandbox Code Playgroud)

c# json.net devextreme entity-framework-core asp.net-core-mvc

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

在 HTML5 画布中定位文本

我正在尝试将文本放在canvas元素内。在此示例中,我想将 48 像素高的文本放入 64 像素高的画布中。因此,文本应占画布高度的 3/4。然而,在我尝试过的所有内容中,文本只占据画布的上部,并且随着我制作的更大而被挤压到那个空间中。

如果我从 0, 0 开始;画布中什么也没有出现,所以我移到 0, 64 开始。这会将文本放在里面,但如前所述缩放比例已关闭。如果我一直到 0, 128 - 这对我来说甚至没有意义 - 文本被移动到画布的底部,但仍然被压扁。这里肯定有一些我不明白的定位。如何在画布中获取我指定的高度的文本?

let canvas = document.getElementById("cv");
let ctx = canvas.getContext("2d");

ctx.font = "48px Arial";
ctx.fillText("Hello World", 0, 64);
Run Code Online (Sandbox Code Playgroud)
<canvas id="cv" style="border: 1px solid black; height: 64px; width: 300px;"></canvas>
Run Code Online (Sandbox Code Playgroud)

html javascript css html5-canvas

0
推荐指数
1
解决办法
599
查看次数