假设我有以下回购模式:
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) 假设您的实体中有这些类.
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)
并且您有一个用户界面来更新Parent它Children,这意味着如果用户添加新的,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次,得到的答案很复杂.那么实际上可以做到这一点而不会变得复杂吗?这个问题可以通过分离之间的用户界面解决问题Parent和Child,但我不想因为两者合并Child,并 …
我是 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从我的组件传递?
我有一个小型网站(MVC5),其中包含"联系我们"功能,今天早上我发现我有来自同一IP的数百封电子邮件.我从数据库查询结果,所有'em只是一堆奇怪的字符串和一些脚本/ SQL注入.
我已经在我的数据库(SQL Server 2014)上使用参数,并对所有用户输入进行白名单过滤.只是想知道我是否应该担心?
Joey'"
Joey\\'\\"
Joey'"'"'"'"
Joey AND 1=1 --
Joey AND 1=2 --
Joey" AND 1=1 --
Joey" AND 1=2 --
Joey'
Joey
Joey\'
Joey
Joey" 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 --
" AND 1=1 --
" AND 1=2 --
Joey''
Joey' UNION SELECT 8, table_name, 'vega' FROM information_schema.taables WHERE taable_name like'%
javascript:vvv002664v506297
vbscript:vvv002665v506297
" onMouseOver=vvv002666v506297
" …Run Code Online (Sandbox Code Playgroud) 基本上我有这个类,它代表我的数据库 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) 所以我想通过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) 我有一堆页面HttpPost请求,我从我的同事那里扫描了我的网站Acunetix(我认为).结果说HTML form without CSRF protection (9).建议是Same-origin policy通过实现Token 来使用.我的问题 :
Token在每个POST请求中使用它是否值得?我只Token在POSTLogIn,Register,Transaction等敏感请求中使用.Acunetix只列出我的几页CSRF可能存在的风险POST,检测模式如何工作?任何帮助将不胜感激.
我有一个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
我想将 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)
任何帮助将不胜感激。
我想更新生产数据库上的数据库架构。通常在我的开发中,我只做Add-Migration和Update-Database。
我的问题是如何生成脚本以便我可以在生产环境中执行之前手动通读查询?我试过了,Update-Database -script但它给了我No pending explicit migrations。
entity-framework entity-framework-5 entity-framework-migrations
asp.net-mvc ×7
c# ×6
security ×2
ajax ×1
automapper ×1
entity-framework-migrations ×1
excel ×1
javascript ×1
jquery ×1
npoi ×1
react-hooks ×1
reactjs ×1
repository ×1
single-responsibility-principle ×1
sql ×1
sql-server ×1