Tag: asp.net mvc 4

在asp net mvc 5中使用Session变量进行授权

所以我的项目要求发生了变化,现在我认为我需要构建自己的动作filter。 所以,这是我当前的登录控制器: public class LoginController : Controller { // GET: Login public ActionResult Index() { return View(); } [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(LoginViewModel model) { string userName = AuthenticateUser(model.UserName, model.Password); if (!(String.IsNullOrEmpty(userName))) { Session[“UserName”] = userName; return View(“~/Views/Home/Default.cshtml”); } else { ModelState.AddModelError(“”, “Invalid Login”); return View(“~/Views/Home/Login.cshtml”); } } public string AuthenticateUser(string username, string password) […]

ViewModel在action方法中获取null值

我正在使用ViewModel来检索控制器操作中输入的数据。 但ViewModel在其属性中获取空值。 我正在创建一个局部视图 在该局部视图中,我通过绑定ViewModel创建下拉列表 ,然后我在其他视图中 渲染该局部视图 以下是我的代码 我的ViewModel: public class LookUpViewModel { RosterManagementEntities rosterManagementContext = new RosterManagementEntities(); public LookUpViewModel() { tblCurrentLocations = from o in rosterManagementContext.tblCurrentLocations select o; tblStreams = from o in rosterManagementContext.tblStreams select o; } [Required] public virtual IEnumerable tblCurrentLocations { get; set; } [Required] public virtual IEnumerable tblStreams { get; set; } […]

在Foreach循环中默认选中设置RadioButtonFor()

我使用@Html.RadioButtonFor扩展方法有一个奇怪的行为。 我正在使用foreach循环来创建RadioButton和By三元运算符列表。 我试图设置一个尊重条件的人来检查,但它始终是最后一个被检查的人。 我搜索了类似的问题,但我不确定是否找到了什么。 而且我不想创建/使用自定义的RadioButtonList 。 在我的视图中我的代码: @foreach (var item in Model.Entities) { @Html.RadioButtonFor(m => item.Default, item.EntityId, new { @checked = item.Default ? “checked” : “” })//item.Default is a bool and there is only one set to True } 但是在我的浏览器中,即使item.Default为false它也始终是最后创建的。 那有什么东西

您如何检测Api控制器中的当前浏览器?

我正在尝试使用MVC4在我的程序中的一个Api控制器中检测当前的Web浏览器。 无论我到哪里,人们都会说要使用Request.Browser ,但是我无法Request.Browser工作。 有什么建议或者我有什么东西可以忽略吗?

使用Umbraco 7的异步控制器操作返回字符串

是否可以在Umbraco SurfaceController(和UmbracoApiController)中使用异步操作 我尝试了以下代码 public async Task HandleLogin(LoginViewModel model) { await Task.Delay(1000); return PartialView(“Login”, model); } 虽然它在调用动作时正确编译,但是一旦命中await,动作似乎就会返回,并返回一个字符串 System.Threading.Tasks.Task`1 [System.Web.Mvc.ActionResult] 控制器当然inheritance自SurfaceController ,我想知道这是不是问题? 如果这不可能,是否有任何变通方法来实现异步操作行为? 任何帮助都会感激不尽!

ASP.NET MVC自定义视图路由

我们的解决方案层次结构如下: 控制器\目录\查看 例如:Controllers \ DataAnalysis \ DataRetrieve 现在我想映射路由,以便当用户只在URL中键入视图的名称时,它会自动将url映射到相应的控制器 IE:localhost:1234 \ DataAnalysis \ DataRetrieve 应映射到 查看\ DataAnalysis \ DataRetrieve \ Index.cshtml 同样,任何包含操作的url请求都应检索相应的视图 IE:localhost:1234 \ DataAnalysis \ DataRetrieve \ TestAction 应映射到 查看\ DataAnalysis \ DataRetrieve \ TestAction.cshtml 目前,我们正在使用默认路由 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}”, defaults: new { controller = “Login”, action […]

ASP.NET Web.api如何处理名称以GET开头的两个方法?

我正在查看Microsoft的以下教程。 根据本教程, 在第一个示例中,“products”匹配名为ProductsController的控制器。 该请求是一个GET请求,因此框架在ProductsController上查找名称以“Get …”开头的方法。 此外,URI不包含可选的{id}段,因此框架会查找没有参数的方法。 ProductsController :: GetAllProducts方法满足所有这些要求。 如果有两种方法,比如GetAllProducts()和GetSoldProducts(),会发生什么? 两者都没有参数。 您的第一个Web API教程

使用ELMAH配置自定义授权

如何在没有默认ASP.NET授权角色管理器的情况下将ELMAH配置为仅显示给某些人? 我(以及许多其他人,我认为)使用我自己的授权逻辑并从零开始构建我的项目,而不使用提供的模板。 我想记录错误,但似乎无法配置ELMAH(以某种方式覆盖function)使其与其他授权一起工作,甚至使其仅适用于特定的IP地址。 由于我可以访问web.config我尝试更改这些值,以便默认情况下不显示elmah。 当我想查看错误时将它们从true切换为false并查看错误,然后切换回来。 但似乎当我更改这些值时,所有日志都会被删除。 我能做什么?

ASP.NET Web API中的XML命名空间

我目前正在开发一个项目,要求我从端点和JSON输出XML。 我有以下型号: [DataContract(Namespace=”http://www.yale.edu/tp/cas”)] [XmlType(“serviceResponse”)] [XmlRoot(Namespace=”http://www.yale.edu/tp/cas”)] public class ServiceResponse { [XmlElement(“authenticationSuccess”)] public AuthenticationSuccess Success { get; set; } [XmlElement(“authenticationFailure”)] public AuthenticationFailure Failure { get; set; } } 当success不为null时,输出如下: 现在,我可以看到,显然,我没有为命名空间分配前缀,我告诉元素是其中的一部分。 我的问题是我找不到使用媒体格式化程序在MVC4中添加名称空间前缀的地方。 我在global.asax中有以下内容: GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true; GlobalConfiguration.Configuration.Formatters.XmlFormatter.RemoveSerializer(typeof(Models.ServiceResponse)); GlobalConfiguration.Configuration.Formatters.XmlFormatter.SetSerializer(typeof(Models.ServiceResponse), new Infrastructure.NamespaceXmlSerializer(typeof(Models.ServiceResponse))); 我创建了一个基于XmlSerializer的自定义序列化程序,试图拦截写入请求并在那里添加命名空间列表。 这个方法的问题是,现在我在每个可重写方法中都有断点,并且在序列化时没有一个断点导致我相信我的序列化器没有被使用。 是否有一些内置的方法来完成我想要做的事情,或者我是否在重新实现XmlMediaTypeFormatter以在序列化对象时传入命名空间?

如何在ASP.Net MVC中将RDLC报告与ReportViewer Control一起使用?

我是ASP.Net MVC的新手。 我需要在MVC中显示基于RDLC的报告。 基本上我的要求以及我所做的是: – 我有一个inheritanceAPIController的ReportController,它有一个返回DataSet的方法。 此DataSet正被发送到RDLC文件。 为此,我已完成以下操作,但无法使报告生效。 我创建了一个名为ReportParameter的模型类,如下所示: public class ReportParameter { public DateTime DateFrom { get; set; } public DateTime DateTo { get; set; } } 我有以下控制器ReportViewController: public class ReportViewController : Controller { static readonly ReportController ctrl = new ReportController(); public ActionResult GenerateReport() { return View(); } [HttpPost] public ActionResult GenerateReport(ReportParameterSalesOrder param) { […]