我将值存储在正在测试的控制器操作的会话中。我已经阅读了几篇关于如何模拟会话的文章,并且我正在尝试实现 Milox 对“在单元测试中设置 httpcontext 当前会话”的回答。但是当我钻取Locals | this | base | HttpContext Sessions仍然为空并且设置会话变量时测试失败并出现空引用异常HttpContext.Session["BsAcId"] = vM.BusAcnt.Id;
这是有效的生产代码。 vM.BusAcnt.Id返回一个有效的int值,如果我用一个int值替换它,测试仍然会失败,因为它Session是空的,因此不能在其中存储任何值。
我正在使用 MVC5、EF6 以及最新版本的 xUnit、Moq 和 Resharper 测试运行程序。
行动:
public ActionResult Details(int id)
{
var vM = new BusAcntVm();
vM.BusAcnt = _db.BusAcnts.FirstOrDefault(bA => bA.Id == id);
if ((User.IsInRole("Admin"))) return RedirectToAction("Action");
HttpContext.Session["BsAcId"] = vM.BusAcnt.Id;
return View(vM);
}
Run Code Online (Sandbox Code Playgroud)
模拟助手:
public static class MockHelpers
{
public static HttpContext FakeHttpContext()
{
var httpRequest = new HttpRequest("", "http://localhost/", …Run Code Online (Sandbox Code Playgroud) 执行时密码是否经过哈希处理?UserManager.CreateAsync(user, model.Password);。如果是这样,MVC 5 版本 5.2.2.0 项目中的哈希默认情况下是如何完成的?还是SHA1+salt+base64吗?类似的东西?:
internal string EncodePassword(string pass, string salt)
{
byte[] bytes = Encoding.Unicode.GetBytes(pass);
byte[] src = Convert.FromBase64String(salt);
byte[] dst = new byte[src.Length + bytes.Length];
byte[] inArray = null;
Buffer.BlockCopy(src, 0, dst, 0, src.Length);
Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
inArray = algorithm.ComputeHash(dst);
return Convert.ToBase64String(inArray);
}
Run Code Online (Sandbox Code Playgroud) 在查看 Scott Allen 在 Plural Sight 上制作的教程视频时:
使用 ASP.NET MVC 4和ASP.NET MVC 5 基础知识构建应用程序
我遇到了两个不同的术语:AJAX Helper和async/await。
对我来说,“听起来”两者都在异步模式下运行(如果我错了,请纠正我)。每个之间到底有什么区别?
由于某种原因,EmailCode 未显示在有效的两因素身份验证提供程序中。然而,在我删除 PhoneCode 之前,它现在什么也没有显示。我已经调试过,它显示在 UserManager 下,但由于某些奇怪的原因 GetValidTwoFactorProvidersAsync 没有检索它。我已经尝试通过绕过该方法并手动检索值来手动添加它,但随后它会抛出 Microsoft.AspNet.Identity.EmailTokenProvider 不存在的错误消息。我无法解释为什么这不起作用。
public async Task<ActionResult> SendCode(string returnUrl)
{
var userId = await SignInManager.GetVerifiedUserIdAsync();
if (userId == null)
{
return View("Error");
}
var userFactors = await UserManager.GetValidTwoFactorProvidersAsync(userId);
var factorOptions = userFactors.Select(purpose => new SelectListItem { Text = purpose, Value = purpose }).ToList();
return View(new SendCodeViewModel { Providers = factorOptions, ReturnUrl = returnUrl });
}
Run Code Online (Sandbox Code Playgroud)
身份配置
manager.RegisterTwoFactorProvider("EmailCode", new Microsoft.AspNet.Identity.EmailTokenProvider<SystemUser>
{
Subject = "SecurityCode",
BodyFormat = "Your security code is {0}"
});
manager.EmailService = new …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc two-factor-authentication asp.net-mvc-5 asp.net-identity-2
我有一组数据显示在表格的行中。每行的末尾是一个单元格,其中包含一组可以在该行上执行的操作(查看、打印、编辑、删除等)
目前,我将这些操作作为简单链接,由控制器作为 GET 请求进行处理,但是我需要将它们更改为 POST 请求以满足最佳实践。
我的视图生成的当前 HTML 是这样的(为了清晰起见,缩短了):
<table class="table table-hover">
<tr class="row">
<th>Date</th>
<th>Actions</th>
</tr>
<tr class="row">
<td>03/10/2014</td>
<td>
<a href="/Grid/Details?mainid=123">Details</a> |
<a href="/Grid/Print?mainid=123">View</a> |
<a href="/Grid/Delete?mainid=123">Delete</a>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
当我用表单、隐藏字段和按钮替换删除链接时,无论页面宽度如何,按钮都会放置在“新行”上。视图使用以下代码:
@using (Html.BeginForm("Delete", "Grid"))
{
@Html.Hidden("MainID", Model.MainID)
<button type="submit" class="btn-link" style="border-width: 0px; padding: 0px;">Delete</button>
}
Run Code Online (Sandbox Code Playgroud)
生成以下 HTML:
<a href="/Grid/Details?mainid=123">Details</a> |
<a href="/Grid/Print?mainid=123">View</a> |
<form action="/Grid/Delete" method="post">
<input id="MainID" name="MainID" type="hidden" value="123" />
<button type="submit" class="btn-link" style="border-width: 0px; padding: 0px;">Delete</button>
Run Code Online (Sandbox Code Playgroud)
看起来像这样:

我尝试用 a 包裹表格,<div class="form-group">但这没有什么区别。有没有办法使链接样式按钮与其余链接保持在同一“行”上?
asp.net-mvc twitter-bootstrap asp.net-mvc-5 twitter-bootstrap-3
这是一个奇怪的(无论如何对我来说)查询字符串问题。
\n\n我正在使用具有网络序列号验证功能的安装工具。基本上,安装会将用户的电子邮件和序列号传递到网页(或 MVC 的控制器方法),并采用查询字符串参数并神奇地验证安装。
\n\n参数之一是查询字符串中传递的电子邮件地址。
\n\n我最近有一位用户使用 \xe2\x80\x98+\xe2\x80\x99 电子邮件地址来购买订阅。一切都很顺利,直到他去安装产品并必须通过验证屏幕。
\n\n经过一番挖掘后,我发现而不是接收 \n \xe2\x80\x98joe+foo@gmail.com\xe2\x80\x99
\n\n验证码接收 \n \xe2\x80\x98joe foo@gmail.com\xe2\x80\x99
\n\n当然,这个空格会破坏验证尝试,因为他的电子邮件地址现在是错误的。
\n\n我已经与安装工具公司(高级安装程序,地球上最好的安装工具)交谈过,他们声称(并且我相信他们)电子邮件已正确发送。
\n\n那么这让我如何让 asp.net mvc 查询字符串解析器为该特定参数做正确的事情并将带有“+”的字符串传递给控制器方法?
\n\n如果重要的话,那就是 asp.net mvc 5。
\n\n感谢您提供的任何帮助。
\n\n更新
\n\n问题在于发送,而不是接收。加号最终未编码,因此当查询字符串解析器处理它时,它会转换为空格=。
\n\n因此,我正在寻找一种为该特定 URL(并且仅该 URL)自定义查询字符串解析器的方法。
\n\n修复的快捷方式是在电子邮件参数中用加号替换空格。很简单,但我讨厌代码中的这种黑客行为,如果我需要它,我更愿意让它使用自定义的解析器,而我可以以任何方式插入它。
\n\n您可以自定义 asp.net mvc 中的几乎所有其他内容,因此我想知道是否有一种方法可以以某种自定义方式进行查询字符串解析。
\n我有一个要求,我必须映射以下网址
/amer/us/en/ = Home controller
/amer/us/en/login/index = Home controller
/amer/us/en/confirmation = Confirmation controller
Run Code Online (Sandbox Code Playgroud)
以及常规的默认操作。
例如,如果用户去
http:\\test.com --> http://test/home/index
http:\\test.com/amer/us/en/login/index --> http://test/home/index
http:\\test.com/amer/us/en/ --> http://test/home/index
Run Code Online (Sandbox Code Playgroud)
我正在研究属性路由,因此我在 HomeController 中添加了以下代码
[RoutePrefix("amer/us/en/")]
[Route("{action=index}")]
public class HomeController : Controller
{
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误
The route prefix 'amer/us/en/' on the controller named 'Home' cannot begin or end with a forward slash,并且默认路由现在不起作用,因此http://test.com未加载任何内容。下面是我的默认 RouteConfig 类。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = …Run Code Online (Sandbox Code Playgroud) 当我在 ASP.NET MVC 5 项目中使用“数据库优先”方法创建模型时,并非所有来自 SQL Server 的表都在我的项目中创建。
我已一步步遵循本文中的所有内容: https://www.asp.net/mvc/overview/getting-started/database-first-development/creating-the-web-application
ASP.NET MVC 项目模型:
SQL Server 数据库:
我的 SQL Server 数据库中有三个表尚未在模型中创建。该表是AspNetUserRoles、ContactsAddresses 和 IssuesItems
以下是三个缺失表的设计:
AspNet用户角色:
联系地址:
问题项目:
所有三个缺失的表都是在 SQL Server 中创建的,以消除多对多关系,并且它们都有两个连接两个表的主键。
为什么会发生这种情况以及我可以采取什么措施来解决它?
谢谢。
sql-server asp.net-mvc entity-framework entity-framework-6 asp.net-mvc-5
我目前正在使用SignalR聊天。我有一个表单,用户将把特定的订单加载到表单中。情况是其他人staff members可能会搜索该订单号,因此我只希望这些人参与聊天。目前,如果您加载该网站,每个人都在一个hub名为ChatHub.
聊天中心.cs:
public class ChatHub: Hub {
public void Send(string name, string message) {
// Call the addNewMessageToPage method to update clients.
Clients.All.addNewMessageToPage(name, message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
聊天.cshtml:
@section scripts {
<!--Script references. -->
<!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.0.3.min.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
<!--SignalR script to update the chat page and …Run Code Online (Sandbox Code Playgroud) 我不明白使用函数的原因是什么OnModelCreating?
当我可以做类似的事情时
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int Id { get; set; }
public Int LanguageId { get; set; }
[ForeignKey("LanguageId")]
public Language Language { get; set; }
Run Code Online (Sandbox Code Playgroud)
也许我错了,但是当我读到这一点时,它被解释为它是针对多对多关系的。那么为什么不做这样的事情呢。
ICollection<User> Users
Run Code Online (Sandbox Code Playgroud) asp.net-mvc-5 ×10
asp.net-mvc ×8
asp.net ×4
c# ×3
ajax ×1
email ×1
hash ×1
jquery ×1
mocking ×1
session ×1
signalr ×1
sql-server ×1