我正在为我的MVC应用程序的错误处理代码中开发自定义错误页面.但我不清楚我打算涵盖哪些HTTP状态代码.
问题:是否存在应该满足的HTTP状态代码的典型列表?
很多文章解释了如何进行MVC错误处理和自定义错误页面,但似乎只在其错误处理代码中显示了几个HTTP状态代码:403,404和500.那么HTTP状态代码:408就是一个例子?这应该涵盖吗?那吨其他状态代码 - 维基上的HTTP状态代码怎么样?
这可能听起来像一个愚蠢的问题,但我真的不知道答案,也无法找到相关信息.我在这里遗漏了什么,即应该只涵盖一部分状态代码吗?
如果它有帮助,下面就是我为MVC错误处理所做的事情.这段代码(到目前为止,我完成的测试很少)涵盖了404,以及所有50x类型的异常:
1在web.config中,以及我想要涵盖的每个HTTP状态代码的条目
<httpErrors errorMode="Custom" existingResponse="Replace" >
<remove statusCode="403" />
<remove statusCode="404" />
<remove statusCode="500" />
<error statusCode="403" responseMode="ExecuteURL" path="/Error/Forbidden" />
<error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
<error statusCode="500" responseMode="ExecuteURL" path="/Error" />
</httpErrors>
Run Code Online (Sandbox Code Playgroud)
2错误控制器
namespace MyApp.Controllers
{
public class ErrorController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Forbidden()
{
return View();
}
public ActionResult NotFound()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
3用户友好的错误页面:
/Views/Shared/Index.cshtml
/Views/Shared/Forbidden.cshtml
/Views/Shared/NotFound.cshtml
Run Code Online (Sandbox Code Playgroud)
4 ELMAH用于记录
2015年11月2日的进一步调查结果 …
我需要帮助来为用户名构建正则表达式.
用户名分为三个部分.第一个字符,中间组,最后一个字符.
以下是我必须遵循的规则:
(a-z)(a-zA-Z0-9)(0-9)一些例子:
hTes38 (i.e. h Tes3 8)
j347k6 (i.e. j 347k 6)
atksde21D2 (i.e. a tksde21D 2)
Run Code Online (Sandbox Code Playgroud)
以下是我到目前为止的情况,几乎就是:
^[a-z][a-zA-Z0-9]\w{1,}[0-9]$
Run Code Online (Sandbox Code Playgroud)
但中间组不正确,我不知道如何强制执行'必须包含一个字母和一个数字'规则.
在线查看一些MVC示例,我看到通常在控制器中DbContext变量被声明为私有成员变量(即全局)并且可以被所有方法访问.
但是,我最近遇到了一篇关于ASP.NET身份的文章,并且在控制器中注意到,DbContext是在每个方法中声明的(需要它).
这种方法有安全上的好处吗?或许限制安全对象的生命周期以获得更好的整体安全性?!?!
如果没有,那么我看到第一种方法更有效,其中数据库上下文在控制器加载时被实例化.
以下是我能找到的关于DbContext的全部内容,但没有什么可以真正回答我的问题.
我正在尝试在我的架构中实现依赖注入(MVC,DDD - 域模型,存储库).我的架构包括ASP.NET Identity 2.0.
在这个阶段,我不希望DI控制任何Identity 2.0对象(UserAdminController,RolesAdminController ......).我更喜欢DI之外的安全对象.在这个阶段,将标识对象集成到DI中看起来非常困难.我好好看看是否有人已经这样做了,所以我可以阅读并学习如何做到这一点.我找不到任何东西.(找到一个贴近但没有解决的帖子).
无论如何,我遵循Simple Injector MVC实现(参见下面的标准代码),并尝试了很多东西,我相信问题在于我调用RegisterMvcControllers.
如果我错了,请纠正我,但是这个声明会将所有控制器的名称用"控制器"后固定.
问题:如何选择使用Simple Injector注册哪些控制器?(这是手动注册吗?)
任何帮助都会非常感激,因为我今天大部分时间都在试图解决所有这些问题,然后继续下一步,即实现DI,并实例化我的对象.
...
...
...从Application_Start()调用
// Create a Simple Injector container
var container = new Container();
// Configure the container
InitializeContainer(container);
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
// Verify the container's configuration
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
private static void InitializeContainer(Container container)
{
container.Register<MyService1>();
container.Register<IMyRepositoryA, MyRepositoryA>();
// Trying to include Identity into Simple Injector - please ignore
container.Register<IUserStore<ApplicationUser>>(() => new UserStore<ApplicationUser>(new ApplicationDbContext()));
}
Run Code Online (Sandbox Code Playgroud) 根据Simple Injector文档,我正在为MVC应用程序实例化我的存储库对象:
container.RegisterPerWebRequest<IUserRepository, SqlUserRepository>();
container.RegisterPerWebRequest<IOrderRepository, SqlOrderRepository>();
Run Code Online (Sandbox Code Playgroud)
但我刚刚发现文档还说明:
与Simple Injector的默认行为相反,这些扩展方法确保处理已创建的服务(当此类实例实现IDisposable时).
https://simpleinjector.codeplex.com/wikipage?title=ObjectLifestyleManagement#PerWebRequest
问题:这是否意味着在使用RegisterPerWebRequest时,我的对象需要实现IDisposable,以便它们在Web请求结束时(即代码下方)处理?
旁注:我相信使用WebRequestLifestyle,RegisterInitializer,RegisterForDisposal,也需要实现IDisposable的对象.
下面的示例代码(接口和实现).
public interface IUserRepository : IDisposable
{
...
}
public class SqlUserRepository : IUserRepository, IDisposable
{
...
...
...
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Run Code Online (Sandbox Code Playgroud) 在MVC Form Post中使用隐藏字段时遇到问题.当通过HTML Helper生成隐藏字段时,它不会在回发期间保留它的值.但是当使用HTML标签时,它可以正常工作.
不幸的是,这个人花了我一整天的时间来完成这项工作.
这是我正在做的...(原谅任何拼写,重新输入的代码为SO):
查看模型
public class SomeViewModel
{
public int MyProperty1 { get; set; }
public int MyProperty2 { get; set; }
public int MyProperty3 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
发布方法
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult MyActionMethod(SomeViewModel someViewModel, string command)
{
...
...
// someViewModel.MyProperty1
...
...
}
Run Code Online (Sandbox Code Playgroud)
视图
@using (Html.BeginForm("MyActionMethod", "SomeController", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.MyProperty1)
<div class="col-md-2">
<input type="hidden" value=@Model.MyProperty1 name="MyProperty1" />
<input type="submit" name="command" value="Button1" …Run Code Online (Sandbox Code Playgroud)