我正在使用ASP.NET MVC,我需要设置一个会话变量Application_BeginRequest.问题是,此时对象HttpContext.Current.Session始终是null.
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.Session != null)
{
//this code is never executed, current session is always null
HttpContext.Current.Session.Add("__MySessionVariable", new object());
}
}
Run Code Online (Sandbox Code Playgroud) 刚刚开始使用MVC 2,我注意到他们使用的初学者模板
<%: Html.ActionLink("Home", "Index", "Home")%>
Run Code Online (Sandbox Code Playgroud)
我确信在MVC 1中它是
<%= Html.ActionLink("Home", "Index", "Home")%>
Run Code Online (Sandbox Code Playgroud)
它们是一样的吗?如果是这样,为什么从等号变为冒号.
我很喜欢 MVC 2.整个过程非常适合网络.
然而,有一项功能,我无法哄骗这个Html.DisplayFor()功能:
<@ Page Inherits="ViewPage<IEnumerable<Foo>>">
<% foreach(var item in Model) { %>
<%: Html.DisplayFor(item.BarBaz) %>
<% } %>
Run Code Online (Sandbox Code Playgroud)
我需要能够使用DisplayTemplate来获取此值.有没有办法做到这一点?
有人可以请我澄清一些事情.在我的ASP.NET MVC 2应用程序中,我有一个BaseViewModel包含以下方法的类:
public virtual IDictionary<string, object> GetHtmlAttributes<TModel, TProperty>
(Expression<Func<TModel, TProperty>> propertyExpression)
{
return new Dictionary<string, object>();
}
Run Code Online (Sandbox Code Playgroud)
我们的想法是每个子视图模型都可以覆盖此方法,并根据某些逻辑提供一组合适的html属性,以便在视图中呈现:
<%: Html.TextBoxFor(model => model.MyProperty, Model.GetHtmlAttributes
(model => model.MyProperty)) %>
Run Code Online (Sandbox Code Playgroud)
但是当在上面的行中使用时,当我点击视图时出现编译错误:
方法'
...BaseViewModel.GetHtmlAttributes<TModel,TProperty> Expression<System.Func<TModel,TProperty>)' 的类型参数不能从用法中推断出来.尝试显式指定类型参数.
我必须做以下事情:
<%: Html.TextBoxFor(model => model.MyProperty, Model.GetHtmlAttributes
<ChildModel, string>(model => model.MyProperty)) %>
Run Code Online (Sandbox Code Playgroud)
我只是在寻找一些关于它如何尝试推断类型的清晰度,在HtmlHelper/TextBoxFor扩展方法中这样做是没有问题的?
是因为HtmlHelper在视图中将自动ViewUserControl与页面顶部指定的类型相同,而我的代码可以用于继承的任何类型BaseViewModel?有可能以这样的方式编写它,它可以推断我的模型/属性类型?
在使用这样的代码(EditorTemplates/UserType.ascx)时,默认的ASP.NET MVC2 Html助手似乎会生成重复的HTML ID:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<UserType>" %>
<%: Html.RadioButton("", UserType.Primary, Model == UserType.Primary) %>
<%: Html.RadioButton("", UserType.Standard, Model == UserType.Standard) %>
<%: Html.RadioButton("", UserType.ReadOnly, Model == UserType.ReadOnly) %>
Run Code Online (Sandbox Code Playgroud)
它产生的HTML是:
<input checked="checked" id="UserType" name="UserType" type="radio" value="Primary" />
<input id="UserType" name="UserType" type="radio" value="Standard" />
<input id="UserType" name="UserType" type="radio" value="ReadOnly" />
Run Code Online (Sandbox Code Playgroud)
这清楚地表明了一个问题.所以我一定是在滥用助手等等.
我可以手动指定id为html属性,但我无法保证它将是唯一的.
所以问题是如何确保RadioButton助手生成的ID对于每个值都是唯一的,并且仍然保留用于生成这些ID 的约定(因此嵌套模型是否受到尊重?(最好不要手动生成ID.)
当我开始使用.NET Webforms时,我找不到要跟随的文件夹结构,因为VS提供了像"App_Code"这样的应用程序文件夹,大多数应用程序示例都在其中放置了"BLL","DAL"等等.
但是现在在MVC中,我检查的每个例子都使用不同的结构,这次没有标准,我没有在Google或SO上找到一个好的解决方案.
所以,也许我们可以分享我们如何组织我们的MVC项目,可以帮助其他人做出自己的想法.这是我使用的中小型项目的结构:
App_Data
Areas
Admin
Controllers
Models
Views
MyAccount
Controllers
Models
Views
Content
Images
Scripts
Styles
Controllers
HomeController.cs
Helpers
ExtensionMethods // I.e. based on HtmlHelper, use "helper" suffix
MenuHelper.cs // to be called as html.Menu()
Utilities.cs // Other generic (static) libraries, no suffix used
Models
ViewModels // for passing models to Views
RegisterViewModel.cs // use "ViewModel" suffix
Customer.cs // to extend models like adding Model Validation
Repositories
CustomerRepository.cs // use "Repository" suffix
Services
CustomerService.cs // use "Service" suffix, …Run Code Online (Sandbox Code Playgroud) asp.net-mvc conventions naming-conventions asp.net-mvc-3 asp.net-mvc-2
我正在创建一个文档库应用程序DocumentController,需要上传库中每个doument的缩略图.我想将"文件上载"字段保留在与其他字段(Title,Description,CategoryId等)相同的"创建/编辑"表单中.
问题是我不确定我是否可以混合或嵌套表单标签
Html.BeginForm("Create", "Document", FormMethod.Post, enctype = "multipart/form-data")
Run Code Online (Sandbox Code Playgroud)
和
Html.BeginForm()
Run Code Online (Sandbox Code Playgroud)
我的观点如下:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Publications.WebUI.Models.DocumentEditViewModel >" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<fieldset>
<legend>Edit
<%= Html.Truncate(Model.Document.Title, 50)%></legend>
<%= Html.ValidationSummary(false) %>
<% using (Html.BeginForm())
{ %>
<div class="editor-label">
<%= Html.LabelFor(model => model.Document.Title) %>
</div>
<div class="editor-field">
<%= Html.HiddenFor(model => model.Document.DocumentId ) %>
<%= Html.ValidationMessageFor(model => model.Document.Title) %>
<%= Html.TextBoxFor(model => model.Document.Title)%>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.Document.DocumentUrl)%>
</div>
<div class="editor-field">
<%= …Run Code Online (Sandbox Code Playgroud) 我正在研究ASP.net MVC应用程序,我有一个关于为我的控制器使用构造函数的问题.
我正在使用Entity Framework和linq来实现所有数据事务的实体.我需要为几乎所有的控制器操作访问我的实体模型.当我第一次开始编写应用程序时,我在每个Action方法的开头创建了一个实体对象,执行我需要的任何工作然后返回我的结果.
我意识到我正在为每个动作方法反复创建相同的对象,所以我为Entity对象创建了一个私有成员变量,并开始在每个控制器的构造函数中实例化它.现在每个方法只引用私有成员变量来完成它的工作.
我仍在质疑自己哪种方式是正确的.我想知道A.)哪种方法最合适?B.)在构造函数方法中,这些对象生存了多长时间?C.)构造函数方法是否存在性能/完整性问题?
谢谢
我有一个从主网站发回的一些JSON帖子中使用的URL:
HTTP://site/Services/api.svc/UpdateItem
我们正在慢慢更新网站到ASP.Net MVC 2,并且不想破坏系统中的任何当前URL.(JavaScript刷新问题)
我删除了/Services/api.svc并将此API调用的逻辑移动到以下Controller中:
HTTP://网站/ LegacyApi /的updateItem
不幸的是,在添加路由时,我似乎无法覆盖api.svc并继续收到404错误.
路线:
routes.MapRoute(
"UpdateItemApi",
"Services/api.svc/UpdateItem",
new { controller = "LegacyApi", action = "UpdateItem" }
);
Run Code Online (Sandbox Code Playgroud)
根据MSDN对这个具体问题,路由应该通过.
非常感谢任何帮助.
标准.aspx页面的路由按预期工作,因此这似乎与.svc文件及其处理方式有关.
asp.net-mvc-2 ×10
asp.net-mvc ×5
c# ×3
forms ×2
asp.net ×1
c#-4.0 ×1
constructor ×1
conventions ×1
expression ×1
file-upload ×1
generics ×1
global-asax ×1
html ×1
html-helper ×1
jquery ×1
linq ×1
radio-button ×1
url-routing ×1