对于动作方法的返回类型,Web API中的一般做法似乎是什么?
像这样返回CLR对象:
public IEnumerable<ContactModel> Get()
{
return _contactService.GetAllForUser();
}
Run Code Online (Sandbox Code Playgroud)
或者将对象包装在HttpResponseMessage:
public HttpResponseMessage Get()
{
IEnumerable<ContactModel> contacts = _contactService.GetAllForUser();
return Request.CreateResponse((HttpStatusCode) 200, contacts);
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢将自己的CLR对象作为返回类型,因为它显然会产生更干净的方法,因为您不必HttpResponseMessage每次都实例化.
如果您RemoteOnly在Web配置中设置了自定义错误- 这是否意味着MVC的应用程序级错误事件global.asax- Application_Error错误时不会触发?
我刚刚注意到,当我的应用程序发生某个错误,并且我正在远程查看该站点时,不会记录任何错误.但是,当我访问服务器上的应用程序并发生相同的错误时,将记录错误.
这是自定义错误配置设置:
<customErrors defaultRedirect="/Error/Application" mode="RemoteOnly">
<error statusCode="403" redirect="/error/forbidden"/>
<error statusCode="404" redirect="/error/notfound"/>
<error statusCode="500" redirect="/error/application"/>
</customErrors>
Run Code Online (Sandbox Code Playgroud)
编辑
只是出于对人们的兴趣 - 我最终完全关闭了自定义错误并处理重定向Application_Error:
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
// ... log error here
var httpEx = exception as HttpException;
if (httpEx != null && httpEx.GetHttpCode() == 403)
{
Response.Redirect("/youraccount/error/forbidden", true);
}
else if (httpEx != null && httpEx.GetHttpCode() == 404)
{
Response.Redirect("/youraccount/error/notfound", true);
}
else
{
Response.Redirect("/youraccount/error/application", true); …Run Code Online (Sandbox Code Playgroud) 我正在向我的数据库添加一个实体,如下所示:
public TEntity Add(TEntity entity)
{
return (TEntity)_database.Set<TEntity>().Add(entity);
}
Run Code Online (Sandbox Code Playgroud)
但是,该DbSet.Add方法不会返回具有Id等的新添加的实体,它只是将我传入的对象返回到我的Add方法.
它是否应该让我回到完整的新实体,包括Id,或者是不可能得到Id之前SaveChanges()被称为?
在我的应用程序中,我有一些基本的用户信息需要在每个页面上显示(名称,配置文件img).目前我只是将_Layout.cshtml页面中的模型设置为一个被调用的类ApplicationBaseModel,整个应用程序中的每个其他视图模型都必须从该类继承,并且每个操作都必须为基本模型设置适当的数据.
我不介意以这种方式进行简单继承,事实是在每一个动作方法中我都必须检索数据并将其存储在视图模型中.我认为这不是一个非常优雅的解决方案.
任何人对解决这个问题的其他方法有什么想法?
我正在使用HtmlHelper在我的视图中创建一个复选框,如下所示:
<%= Html.CheckBoxFor(model => model.SeatOnly, new { checked = "checked" })%>
但是,抛出错误,因为checked是保留关键字.我发现有几个人说你必须使用'保留字前缀'并简单地在属性前放置一个uderscore,如下所示:
<%= Html.CheckBoxFor(model => model.SeatOnly, new { _checked = "checked" })%>
这不会产生错误,但在生成的html中,属性实际上是'_checked',这意味着它不起作用(如果我使用firebug并删除下划线属性然后生效).
在使用的时候有没有人知道这个方法CheckBoxFor呢?
谢谢
模型绑定器是否不支持JSON对象的数组?下面的代码在发送单个JSON域对象作为ajax帖子的一部分时起作用.但是,在发送JSON域对象数组时,action参数为null.
var domains = [{
DomainName: 'testt1',
Price: '19.99',
Available: true
}, {
DomainName: 'testt2',
Price: '15.99',
Available: false
}];
$.ajax({
type: 'POST',
url: Url.BasketAddDomain,
dataType: "json",
data: domains,
success: function (basketHtml) {
},
error: function (a, b, c) {
alert('A problem ocurred');
}
});
Run Code Online (Sandbox Code Playgroud)
这是动作方法:
public ActionResult AddDomain(IEnumerable<DomainBasketItemModel> domain)
{
...
Run Code Online (Sandbox Code Playgroud)
任何想法,如果有可能这样做?
编辑
@Milimetric
您的解决方案有效 但是,这是我的错,但我演示的代码不是我的问题的真实代码,我试图显示更容易理解的等效代码.
我实际上正在创建一个数组,然后交换一些DOM元素并将JSON对象推送到数组上,然后将此数组作为数据发布...
var domains = [];
$(this).parents('table').find('input:checked').each(function () {
var domain = {
DomainName: $(this).parent().parent().find('.name').html(),
Price: $(this).parent().parent().find('.price span').html(),
Available: $(this).parent().parent().find('.available').html() == "Available"
}
domains.push(domain); …Run Code Online (Sandbox Code Playgroud) 我意识到当你尝试在UI线程上做某种网络请求时会发生这个错误,但正如你在下面的代码中看到的那样,我实际上是在AsyncTask中调用Http Get:
public class LeftPaneFragment extends Fragment {
private ImageView _profileImage;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(wj.tweetTab.R.layout.left_pane, container);
_profileImage = (ImageView) view.findViewById(R.id.profileImage);
setUpProfileInfo(view);
return view;
}
private void setUpProfileInfo(View view) {
new SetUpUserInfo().doInBackground();
}
private class SetUpUserInfo extends AsyncTask<Void, Void, Drawable> {
@Override
protected Drawable doInBackground(Void... params) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(_model.UserInfo.ProfileImageUrl);
InputStream inputStream = null;
try {
HttpResponse response = httpClient.execute(request);
inputStream = response.getEntity().getContent();
} …Run Code Online (Sandbox Code Playgroud) 我需要搜索一个可能很大的句子集合,我不知道从哪里开始.
总之,用户将提交搜索短语,例如"如何删除我的帐户",然后我需要转到数据库并与提供的单词匹配.
目前我正在考虑做以下事情:
有人能指出我正确的方向吗?此外,如果有人知道任何图书馆做这种工作将是伟大的.
干杯
我正在使用实体框架代码第一种方法,我正在构建一个提供数据访问的通用Repository类.在这堂课中我想要一个Add(T entity)方法.但是,没有InsertOnSubmit方法作为DbSet<T>类的一部分,如果我尝试使用该Add方法,我得到一个编译时错误:
The type 'TEntity' must be a reference type in order to use it as parameter 'TEntity' in the generic type or method 'System.Data.Entity.DbContext.Set<TEntity>()'
Run Code Online (Sandbox Code Playgroud)
这是方法:
public TEntity Add(TEntity entity)
{
return _database.Set<TEntity>().Add(entity);
}
Run Code Online (Sandbox Code Playgroud)
有人知道解决这个问题的方法吗?
谢谢
有谁知道是否可以将完整的SVG导入PDFKit文档?我可以从文档中看到它有完整的SVG支持,并且有绘制路径的方法等,但我看不到导入完整SVG文档的方法.
c# ×7
.net ×2
asp.net-mvc ×2
ajax ×1
android ×1
checkboxfor ×1
java ×1
javascript ×1
jquery ×1
json ×1
pdfkit ×1
svg ×1