小编sec*_*age的帖子

Why can I not use the spread operator on a class function?

I'm new to JavaScript. Just a question on using the spread operator on a class function. An example:

let personA = {
  name: "Tom",
  testFunction: function() {
    // ...
  }
};
let newArray = [];
newArray.push({ ...personA });
console.log(newArray);
Run Code Online (Sandbox Code Playgroud)

And the output is:

[{ name: 'Tom', testFunction: F}]
Run Code Online (Sandbox Code Playgroud)

But if I use a class, such as:

[{ name: 'Tom', testFunction: F}]
Run Code Online (Sandbox Code Playgroud)

The output is:

[{ name: 'Tom'}]
Run Code Online (Sandbox Code Playgroud)

So the function is missing. Isn't everything in JS an object? So why …

javascript

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

Dbcontext注册为“作用域”或“瞬态”对关闭数据库连接有影响吗

我对ASP.NET MVC中的DI有基本的了解,但是有一个问题困扰我很多,将Dbcontext注册为“作用域”或“瞬态”有什么区别吗?以下是典型的mvc应用程序的一些代码:

public class EmployeeController : Controller
{
    private EmployeeContext _context;

    public EmployeeController(EmployeeContext context)
    {
        _context = context;
    }

    public ActionResult Index()
    {
        return View(context.Employees.ToList());
    }

    ...//other action methods that access context's DbSet
}
Run Code Online (Sandbox Code Playgroud)

假设我们注册EmployeeContext为临时服务。因此,在我们运行该应用程序之后,该应用程序将监听所有传入的请求。假设发生了一个默认的/ Home / Index的http请求,因此EmployeeController需要创建一个新的实例,因此DI将首先提供一个实例EmployeeContext给控制器的构造函数。_context也可用于所有其他操作方法,并且无需在其他任何地方创建新EmployeeContext服务。

因此,在请求完成后,_context也会被处理。那么这与范围服务的效果不一样吗?我们打算将其注册为“瞬态”服务,最后它就像“范围”服务一样工作,因此将Dbcontext注册为“范围”或“瞬态”真的不重要吗?

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

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

asp-route- *中的奇怪行为

在index.cshtml中,我使用锚标记助手作为

<a asp-action="Edit" asp-route-id="@Model.Id" asp-route-firstname="@Model.Name"</a>
Run Code Online (Sandbox Code Playgroud)

并在动作方法中

public IActionResult Edit(string id, string firstname)
{
   // id and firstname are assigned correct values
   // but  RouteData.Values only has three entries which are: controller, action and id, where is firstname?
}
Run Code Online (Sandbox Code Playgroud)

但是我不能通过访问访问firstname值,RouteData.Values["firstname"];而可以通过访问id值RouteData.Values["id"];,为什么它对id有效但对其他自定义属性无效?

c# asp.net-core-mvc tag-helpers

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