Tag: asp.net mvc 5

asp.net MVC – 如何通过不同的存储库类共享同一个SqlConnection实例

我正在创建一个使用MVC5和普通ADO.NET的新项目(仅作为学习练习),我需要创建一个存储库来注册一个模型,该模型包含多个相关对象,这些对象也需要同时创建,并且这些对象在转弯可能需要插入其他物体。 我能想到的最简单的解决方案是有一个庞大的方法(在存储库中)接收父对象的实例(其中包含需要插入的所有相关对象),并且还有一个接收所有相关的存储过程数据作为表值参数,然后使用单个事务插入所有内容。 虽然这看起来似乎是最简单的方法,但我并不是它的忠实粉丝,所以我想知道的是,我是否可以使用任何方式/通用实践来共享为父对象创建的SqlConnection的相同实例与其他相关的对象? 我想也许在相关对象的构造函数中传递SqlConnection对象,这样每个存储库只需要处理插入单个对象的逻辑,但我不确定。 编辑 ——————————- 这是父对象(Model)的存储库,我认为它应该实例化SqlConnection并启动事务 public class ModelRepository : IModelRepository { public int Add(Model entity) { using (var conn = new SqlConnection(ConnectionString)) { conn.Open(); using (var command = conn.CreateCommand()) { command.Transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted); command.CommandText = “up_Model_Insert”; command.CommandType = CommandType.StoredProcedure; command.Parameters.Add(command.CreateParameter(“@pName “, entity.Name)); command.Parameters.Add(command.CreateParameter(“@pDescription”, entity.Description)); //Other parameters… //Call the repositories of the other objects […]

MVC5区域无法正常工作

我的MVC 5应用程序中有两个区域无法正常工作。 当我使用以下链接http://localhost:45970/Admin/Admin ,应用程序加载正确的index.cshtml whicxh位于/Areas/Admin/Views/Admin/Index.cshtml但是当我尝试加载http://localhost:45970/Admin它尝试从/Views/Admin/Index.cshtml加载Index.cshtml文件。 所有搜索结果都说我正在做正确的事情。 我甚至加载了一个示例API项目来查看其中的帮助区域,以确保我正确地执行操作。 这是我的RouteConfig.cs文件 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace BlocqueStore_Web { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }, namespaces: new[] { […]

高效缓存身份相关实体

我正在使用asp.net MVC5和asp.net Identity v2(来自每晚版本),但我认为这个问题仍然适用于Identity V1。 我有一个会员系统,我通过membershipuser表中的字段AspUserId将AspNetUser实体链接到我的membershipuser实体。 public partial class membershipuser { public int id { get; set; } public string full_name { get; set; } public string address { get; set; } public string AspNetUserId { get; set; } ….. } 我想知道在请求的生命周期中缓存您的成员资格用户记录的最佳方法是什么。 这条路: public static class extensions { public static membershipuser GetMember(this System.Security.Principal.IPrincipal User) { […]

ASP MVC 5属性路由没有注册路由

概观 我正在尝试让属性路由与我的api控制器一起工作。 它似乎没有工作,我不确定为什么。 我不确定我是否错过了关键步骤,或者问题是什么。 问题 我正试图点击localhost / api / user / custom?test = 1但是我得到了404(我希望这可以工作) 如果我点击localhost / api / customapi?test = 1我成功进入我的方法 为什么第一个url不起作用? 建立 我的设置如下: CustomController.cs [System.Web.Mvc.RoutePrefix(“api”)] public class CustomApiController : ApiController { [System.Web.Mvc.Route(“user/custom”)] [System.Web.Http.HttpGet] public async Task Get([FromUri] CustomRequest request) { //Work } } WebApiConfig.cs public static class WebApiConfig { public static void Register(HttpConfiguration config) […]

当前上下文中不存在名称“DefaultAuthenticationTypes”

我正在尝试在我的Web应用程序中实现基于角色的授权,如下所示: [HttpPost] [ActionName(“Login”)] public ActionResult Login(LoginViewModel model) { if (ModelState.IsValid) { string userName = model.Username; string[] userRoles = (string[])Session[“UserRoles”]; ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userName)); userRoles.ToList().ForEach((role) => identity.AddClaim(new Claim(ClaimTypes.Role, role))); identity.AddClaim(new Claim(ClaimTypes.Name, userName)); AuthenticationManager.SignIn(identity); return RedirectToAction(“Success”); } else { return View(“Login”,model); } } 我在两行上出错了: 1.ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie); 和: 2.AuthenticationManager.SignIn(identity); 错误1: the […]

MVC5 EF实体显然正在保存,但在检索时为null

我有一个Account类,其中包含Payments清单,其中包含Pending , Sold或Refunded的Products列表。 选择要付费的Products ,我可以将它们添加到Payment对象中,然后将整个对象保存到Account 。 但是,当我在Pay操作中检索帐户时, Account.Payments为空。 这是AddProduct操作: public ActionResult AddProduct(List products) { //var acc = Account.FetchAccountFromIdentity(User.Identity.GetUserName()); var username = User.Identity.GetUserName(); var acc = db.Accounts.First(u => u.EmailAddress == username); var payment = new Payment(); foreach (var product in products) { if (product.IsSelected) { var tempProduct = db.Products.Find(product.ProductID); payment.Products.Add(tempProduct); payment.PaymentStatus = enPaymentStatus.Pending; payment.Gross += tempProduct.Price; […]

如何为ASP.NET MVC 5应用程序设置时区?

我有一个非常大的Web应用程序,它是用ASP.NET MVC 5和MsSql 2008开发的。在我的电脑上,我有+0700 UTC,但在我的共享主机上我有其他时区。 这段代码给了我正确的DateTime。 DateTime utcTime = DateTime.UtcNow; string zoneID = “N. Central Asia Standard Time”; TimeZoneInfo myZone = TimeZoneInfo.FindSystemTimeZoneById(zoneID); DateTime custDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, myZone); Console.WriteLine(custDateTime.ToString()); 不幸的是,我有很多地方,我在那里工作的日期和时间。 我害怕,我忘了改变代码。 有没有简单的方法为我的Web应用程序设置正确的时区? PS我的应用程序的所有用户都有相同的时区。

Visual Studio 2013.3中新MVC5项目的错误

想知道是否有其他人经历过这个以及他们的解决方案是什么。 在Visual Studio 2013中,我创建了一个新的ASP.NET Web应用程序,保留所有默认值 在下一个屏幕中,我选择MVC,为MVC添加文件夹和核心参考,但不是其他两个选项。 身份validation保留在个人用户帐户中 ,我已取消选中云中的主机选项,如下所示。 项目向导完成后,我可以看到它在执行任何其他操作之前有26个错误。 删除一堆这些错误的第一个修复是Views\Account\ _SetPasswordPartial.cshtml和_ChangePasswordPartial.cshtml文件包含无效模型,因此我更改如下: [我的项目名称是WebApplication1 ,替换你自己的值] 在_SetPasswordPartial.cshtml中:从@model WebApplication1.Models.ManageUserViewModel到@model WebApplication1.Models.SetPasswordViewModel 在文件_ChangePasswordPartial.cshtml中:从@model Microsoft.AspNet.Identity.ManageUserViewModel到@model WebApplication1.Models.ChangePasswordViewModel 这使我陷入4个错误,分布在4个文件中 1.第68行,ManageController.cs return View(linkedAccounts); 视图RemoveLogin不存在 2,3。 有两个错误_SetPasswordPartial.cshtml和_ChangePasswordPartial.cshtml抱怨无法解决管理操作,但是当我在浏览器中调试和访问这些URL时,它们工作正常,所以我怀疑它们在某个路径表中。 我安装了R#,所以如果是这样的话,有时可能会出错。 4.最后一个是_RemoveAccountPartial.cshtml在第15行有一个错误,它抱怨帐户控制器中没有Disassociate操作,只要我可以确定这与删除应用程序的其他身份validation提供程序有关。 现在我可以通过添加所需的代码来解决所有这些问题,但它并不适合我,模板不能很好地开箱即用。 是否有新的模板可供使用,或者是否有人在开始正常工作之前如何使模板嗡嗡作响? 编辑2014-11-13我刚刚应用了VS2013.4,这些问题似乎已经修复了。 如果这是人们的问题,那么我建议应用该更新。

System.Web.WebPages中的MethodAccessException将mvc 3迁移到mvc 5

使用Visual Studio 2013,我将混合的Asp.Net Webforms / MVC 3 / Framework 4.0 Web应用程序迁移到Asp.Net Webforms / MVC 5.2.2 / Framework 4.5.1。 我按照这些链接中列出的步骤操作: http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and- Web的API-2 http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/upgrading-signalr-1x-projects-to-20 当我在visual studio中运行应用程序时,我收到此错误: 尝试通过安全透明方法’System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule()’来访问安全关键方法’System.Web.WebPages.ApplicationPart..ctor(System.Reflection.Assembly,System.String)’失败。 我尝试过在StackOverflow上找到的这些建议,但没有解决问题: 清除bin文件夹并重建 安装Nuget包Microsoft.Aspnet.WebHelpers。 确保System.Web.Mvc和System.Web.WebPages的所有web.config引用都指向正确的版本(分别为5.2.2.0和3.0.0.0)。 任何有关如何找到此错误原因的建议都将非常感激。 错误的完整堆栈跟踪: [MethodAccessException: Attempt by security transparent method ‘System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule()’ to access security critical method ‘System.Web.WebPages.ApplicationPart..ctor(System.Reflection.Assembly, System.String)’ failed.] System.Web.WebPages.Administration.SiteAdmin.RegisterAdminModule() +96 System.Web.WebPages.Administration.PreApplicationStartCode.Start() +41 [InvalidOperationException: The pre-application start initialization […]

ASP.NET核心WebAPI 500 IIS 7.5中的内部错误

我很难让这个WebAPI工作。 好吧,使用IIS。 在IIS express中一切正常,但是当我发布它时,特别是1 api请求不起作用。 我正在尝试访问API/[Controller]/{date}/{integer}的URL。 我一直收到500服务器错误。 我的API/[Controller]/{date}其他路线有效。 这是我的API控制器: [Route(“api/[controller]”)] public class PostingsController : Controller { // GET: api/Postings/5 [HttpGet(“{date}”)] public string Get(string date) { return date; } // GET api/Postings/20160407/2 [HttpGet(“{date}/{modeID}”)] public List Get(string date, int modeID) { TruckPosting tp = new TruckPosting(); List truckPostings = tp.GetTruckPostings(date, modeID); return truckPostings; } } 原因可能是我正在尝试返回List ? […]