小编war*_*990的帖子

MVC设计模式,服务层目的?

假设我有以下回购模式:

interface IGenericRepo<T> where T : class
{
     IEnumerable<T> GetAll();
     T GetById(object id);
     void Insert(T obj);
     void Update(T obj);
     void Delete(T obj);
     void Save();
}

interface ICustRepo : IGenericRepo<Cust>
{
     IEnumerable<Cust> GetBadCust();
     IEnumerable<Cust> GetGoodCust();
}

public class CustRepo : ICustRepo<Cust>
{
     //implement method here
}
Run Code Online (Sandbox Code Playgroud)

然后在我的控制器中:

public class CustController
{
     private ICustRepo _custRepo;

     public CustController(ICustRepo custRepo)
     {
         _custRepo = custRepo;
     }

     public ActionResult Index()
     {
         var model = _custRepo.GetAll();
         return View(model);
     }

     public ActionResult BadCust()
     {
         var model = _custRepo.GetBadCust(); …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc design-patterns entity-framework repository

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

是否真的不可能在EF中更新子集合(也称为非hacky方式)?

假设您的实体中有这些类.

public class Parent
{
    public int ParentID { get; set; }
    public virtual ICollection<Child> Children { get; set; }
}

public class Child
{
    public int ChildID { get; set; }
    public int ParentID { get; set; }
    public virtual Parent Parent { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

并且您有一个用户界面来更新ParentChildren,这意味着如果用户添加新的,Child那么您必须插入,如果用户编辑现有的Child则需要更新,如果用户删除了Child则必须删除.现在很明显,如果您使用以下代码

public void Update(Parent obj)
{
    _parent.Attach(obj);
    _dbContext.Entry(obj).State = EntityState.Modified;
    _dbContext.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)

它将无法检测到内部的更改,Child因为EF无法检测到导航属性中的更改.

我一直在问这个问题4次,得到的答案很复杂.那么实际上可以做到这一点而不会变得复杂吗?这个问题可以通过分离之间的用户界面解决问题ParentChild,但我不想因为两者合并Child,并 …

c# entity-framework entity-framework-6

11
推荐指数
2
解决办法
9921
查看次数

在非 React 组件中使用钩子的替代方法是什么?

我是 React 新手,我有这个功能。

    import Axios from "axios";
    
    const UserService = {
        getUserRole: (access_token: string = "") => {
            return Axios({
                method: "get",
                url: "https://<url>/user/role",
                headers: {
                    "Authorization": `Bearer ${access_token}`
                }
            }).then((response) => {
                return response.data;
            }).catch((error) => {
                console.log(error);
            });
        }
    }

export default UserService
Run Code Online (Sandbox Code Playgroud)

经常被另一个组件使用getUserRole,例如

import UserService from "../../../services/authentication/userService";
import { useAuth } from "react-oidc-context";

...

const auth = useAuth();
UserService.getUserRole(auth.user?.access_token);
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我必须不断地传递access_tokenfrom useAuth。有什么方法可以useAuth在我的内部调用UserService,这样我就不必不断地access_token从我的组件传递?

javascript reactjs react-hooks

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

我在我的数据库中发现了很多奇怪的字符串,有人试图进入我的网站?

我有一个小型网站(MVC5),其中包含"联系我们"功能,今天早上我发现我有来自同一IP的数百封电子邮件.我从数据库查询结果,所有'em只是一堆奇怪的字符串和一些脚本/ SQL注入.

我已经在我的数据库(SQL Server 2014)上使用参数,并对所有用户输入进行白名单过滤.只是想知道我是否应该担心?

Joey'&quot;
Joey\\'\\&quot;
Joey'&quot;'&quot;'&quot;'&quot;
Joey AND 1=1 -- 
Joey AND 1=2 -- 
Joey&quot; AND 1=1 -- 
Joey&quot; AND 1=2 -- 
Joey'
Joey
Joey\'
Joey
Joey&quot; UNION SELECT 8, table_name, 'vega' FROM information_schema.tables WHERE table_name like'%
1 AND 1=1 -- 
1 AND 1=2 -- 
' AND 1=1 -- 
' AND 1=2 -- 
&quot; AND 1=1 -- 
&quot; AND 1=2 -- 
Joey''
Joey' UNION SELECT 8, table_name, 'vega' FROM information_schema.taables WHERE taable_name like'%
javascript:vvv002664v506297
vbscript:vvv002665v506297
&quot; onMouseOver=vvv002666v506297
&quot; …
Run Code Online (Sandbox Code Playgroud)

sql sql-server security asp.net-mvc sql-injection

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

使用 Automapper 将字符串映射到 List&lt;string&gt;,反之亦然

基本上我有这个类,它代表我的数据库 1:1

public class User
{
    public int UserID { get; set; }
    public string Username { get; set; }
    public string Role { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有这个视图模型

public class UserEditViewModel
{
    public UserEditViewModel()
    {
        Roles = new List<string>();
    }

    public int UserID { get; set; }
    [Required]
    public string Username { get; set; }
    [Required]
    public List<string> Roles { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何在这两个之间进行映射。我当前的设置:

Mapper.CreateMap<UserEditViewModel, User>().ReverseMap();
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc automapper

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

如何通过jquery ajax发送AntiForgeryToken(CSRF)和FormData

所以我想通过AJAX POST与fileUpload一起使用AntiForgeryToken.这是我的代码:

视图

@using (Html.BeginForm("Upload", "RX", FormMethod.Post, new {id = "frmRXUpload", enctype = "multipart/form-data"}))
{
     @Html.AntiForgeryToken()
     @Html.TextBoxFor(m => m.RXFile, new {.type = "file"})
     ...rest of code here
}

<script>
    $(document).ready(function(){
        $('#btnRXUpload').click(function () {
            var form = $('#frmRXUpload')

            if (form.valid()) {
                var formData = new FormData(form);
                formData.append('files', $('#frmRXUpload input[type="file"]')[0].files[0]);
                formData.append('__RequestVerificationToken', fnGetToken());

                $.ajax({
                    type: 'POST',
                    url: '/RX/Upload',
                    data: formData,
                    contentType: false,
                    processData: false
                })
            }
        })
    })
</script>
Run Code Online (Sandbox Code Playgroud)

调节器

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Upload()
{
    //rest of code here …
Run Code Online (Sandbox Code Playgroud)

c# ajax asp.net-mvc jquery asp.net-mvc-5

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

我应该在每个POST请求中使用ValidateAntiForgeryToken吗?

我有一堆页面HttpPost请求,我从我的同事那里扫描了我的网站Acunetix(我认为).结果说HTML form without CSRF protection (9).建议是Same-origin policy通过实现Token 来使用.我的问题 :

  1. 从性能与安全的角度来看,如果我Token在每个POST请求中使用它是否值得?我只TokenPOSTLogIn,Register,Transaction等敏感请求中使用.
  2. 这可能与标题无关,但是当我有很多页面请求时,为什么pentest软件Acunetix只列出我的几页CSRF可能存在的风险POST,检测模式如何工作?

任何帮助将不胜感激.

c# security asp.net-mvc

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

MVC中的单一责任原则

我有一个MVC项目,具有以下模式

View <-> Controller <-> Service <-> Repository/Entities <-> Database

例如,如果我的数据库中有2个表(Customer和Order),那么我的Repository层中有2个类(这个类映射1:1和我的数据库表,因为我使用的是EF Code First):

public class Customer
{
     [Key]
     public int CustomerID { get; set; }
     public int Name { get; set; }
     //rest of columns here
}

public class Order
{
     [Key]
     public int OrderId { get; set; }
     //rest of columns here
}
Run Code Online (Sandbox Code Playgroud)

然后我有服务:

public class CustomerService : ICustomerService
{
     void AddNewCustomer(Customer obj);
     void GetCustomerOrders(Customer obj);
     //rest of methods here
}

public class OrderService : IOrderService
{
     void GetOrderById(int …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc design-patterns entity-framework single-responsibility-principle

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

返回excel文件而不将其保存在控制器内的服务器中

我想将 Excel 文件(使用 NPOI 库)返回给用户,而无需先将文件保存在服务器中。这是我的代码:

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Report(SalesReportViewModel model)
        {
            if (ModelState.IsValid)
            {
                XSSFWorkbook wb = context.GetReport(model);
                //I have no idea what to return after I got my wb
            }

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

任何帮助将不胜感激。

asp.net-mvc excel npoi asp.net-mvc-5

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

EF - 从以前的迁移生成脚本

我想更新生产数据库上的数据库架构。通常在我的开发中,我只做Add-MigrationUpdate-Database

我的问题是如何生成脚本以便我可以在生产环境中执行之前手动通读查询?我试过了,Update-Database -script但它给了我No pending explicit migrations

entity-framework entity-framework-5 entity-framework-migrations

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