我试图返回一个视图,不会根据我的应用程序可能发生的某些错误向用户发出重定向,我想处理错误+在基本控制器中记录它们,我不希望错误传播到我的Global.asax - Application_Error()方法因为我希望这个方法能够处理我的应用程序中的任何其他错误,例如用户输入虚假URL,有没有人找到解决方法?
注意:我已经离开了我的注释代码,因为我有一些问题的解决方法,这也表明我有可能处理的多个例外...
编辑:如果我在这个OnException覆盖中发出RedirectToAction覆盖一切按预期工作,但我只想返回视图,没有重定向...
我的基本控制器方法是:
protected override void OnException(ExceptionContext filterContext)
{
//dont interfere if the exception is already handled
if (filterContext.ExceptionHandled)
return;
//let the next request know what went wrong
filterContext.Controller.TempData["exception"] = filterContext.Exception;
//log exception
_logging.Error(User.Identity.Name, ExceptionHelper.BuildWebExceptionMessage(filterContext.Exception));
//set up redirect to my global error handler
//if (filterContext.Exception.GetType() == typeof(NoAccessException))
// filterContext.Result = View(new RouteValueDictionary
// (new { area = "", controller = "Error", action = "PublicError" }));
//else {
//Only return view, no need for redirection
filterContext.Result = View(new …Run Code Online (Sandbox Code Playgroud) 我试图将一些非常长的文本插入到字符串prop中 - 它与LinqToSql完全一致,现在我已切换到NHibernate并希望保存相同的实体,但nHibernate会抛出上述异常.
我怎样才能解决这个问题?
最初我的道具被定义为:
Map(x => x.Content, "fT_Content").Nullable();
Map(x => x.Fields, "fT_Fields").Nullable();
Run Code Online (Sandbox Code Playgroud)
现在他们是:这有效,但为什么我必须这样做?
Map(x => x.Content, "fT_Content").CustomSqlType("nvarchar(max)").Length(Int32.MaxValue).Nullable();
Map(x => x.Fields, "fT_Fields").CustomSqlType("nvarchar(max)").Length(Int32.MaxValue).Nullable();
Run Code Online (Sandbox Code Playgroud)
注意:我使用nuget有最新的nhibernate.
对于ref,这里是字段:
public virtual string Content
{
get;
set;
}
public virtual string Fields
{
get;
set;
}
Run Code Online (Sandbox Code Playgroud)
我想避免去生产,突然插入停止在这张桌子上工作....
使用resharper,我们可以点击Ctrl+T
这会打开课程,您可以单击查看课程.
在visual studio 2012(express)中是否有类似的快捷方式/内置?
注意:我说的是没有安装resharper
可能重复:
如何将流保存到文件?
我有一个流对象,可能是一个图像或文件(msword,pdf),我决定以非常不同的方式处理这两种类型,因为我可能想要优化/压缩/调整大小/生成缩略图等.我调用一个特定的方法来将图像保存到磁盘,代码:
var file = StreamObject;
//if content-type == jpeg, png, bmp do...
Image _image = Image.FromStream(file);
_image.Save(path);
//if pdf, word do...
Run Code Online (Sandbox Code Playgroud)
我如何实际保存单词和PDF?
//multimedia/ video?
Run Code Online (Sandbox Code Playgroud)
我看了(可能不够硬)但我找不到任何地方......
如何使用KnockoutJS添加分页?
我目前的代码是:
//assuming jsondata is a collection of data correctly passed into this function
myns.DisplayFields = function(jsondata) {
console.debug(jsondata);
window.viewModel = {
fields: ko.observableArray(jsondata),
sortByName: function() { //plus any custom functions I would like to perform
this.items.sort(function(a, b) {
return a.Name < b.Name ? -1 : 1;
});
},
};
ko.applyBindings(viewModel);
}
Run Code Online (Sandbox Code Playgroud)
我的看法:
<table>
<tbody data-bind='template: "fieldTemplate"'></tbody>
</table>
<script type="text/html" id="fieldTemplate">
{{each fields}}
<tr>
<td> ${ FieldId }</td>
<td>${ Type }</td>
<td><b>${ Name }</b>: ${ Description }</td>
</tr>
{{/each}}
</script> …Run Code Online (Sandbox Code Playgroud) 我正在构建一个流畅的界面,并希望在我的控制器外调用下面的代码...
return RedirectToAction("Activity");
Run Code Online (Sandbox Code Playgroud)
我该如何设计这种方法?我有:
public FluentCommand RedirectOnSuccess(string url)
{
if (IsSuccess)
;// make call here...
return this;
}
Run Code Online (Sandbox Code Playgroud)
注意:IsSuccess设置在这里:
public FluentCommand Execute()
{
try
{
_command.Execute();
IsSuccess = true;
}
catch (RulesException ex)
{
ex.CopyTo(_ms);
IsSuccess = false;
}
return this;
}
Run Code Online (Sandbox Code Playgroud)
我称之为流畅的界面:
var fluent = new FluentCommand(new UpdateCommand(param,controller,modelstate)
.Execute()
.RedirectOnSucess("Actionname");
Run Code Online (Sandbox Code Playgroud) 我正在访问远程数据源,发现组连接导致IQueryable查询转换为IEnumerable,
问题:这会影响性能吗?我想尽可能多地将查询卸载到数据库中,而不是在内存中执行任何操作......
var allusers = repo.All<User>().Where(x => x.IsActive);
var _eprofile = repo.All<Profile>()
.Where(x => x.IsProfileActive)
.Join(allusers, x => x.UserID, y => y.UserID, (x, y) => new
{
eProfile = x,
profile = y
})
.GroupJoin(_abilities, x => x.eProfile.ID, y => y.ID, (x, y) => new QuoteDTO
{
UserID = x.profile.Login,
Email = x.profile.Email,
Tel = x.profile.Tel,
Mobile = x.eProfile.Mobile,
CompanyID = x.profile.CompanyID,
Ability = y.Select(c => new AbilityDTO
{
Name= c.Name
})
});
Run Code Online (Sandbox Code Playgroud)
行:.GroupJoin(_abilities,x => x.eProfile.ID,y => y.ID, …
我应该从域服务过滤我的IQueryable结果吗?
例如......我的3个门户网站(网站)访问相同的域服务层,具体取决于用户的类型,我调用特定的存储库方法并返回结果,
当前存储库层:
IQueryable<Products> GetAllProductsForCrazyUserNow(CrazyUser id);
Products GetAProductForCrazyUserNow(CrazyUser id,product id);
IQueryable<Products> GetProductsForNiceUserNow(NiceUser id);
Products GetProductsForNiceUserNow(NiceUser id,product id);
Run Code Online (Sandbox Code Playgroud)
在存储库层中执行此操作是否最好:
IQueryable<Products> GetAllProducts();
Products GetAProduct(product id);
Run Code Online (Sandbox Code Playgroud)
然后在域服务中,我简单地做过滤器,即
var Niceman = IQueryable<Products> GetAllProducts().Where(u=> u.Name == "Nice");
Run Code Online (Sandbox Code Playgroud)
注意:我有一个只读会话和会话,其中包括存储库层中的CRUD,因此在回答时请记住这一点.
第二个问题:我应该在域服务层进行任何过滤吗?这个层是唯一可以修改实体的层,即Product.Price == 25.00; 这不是委托给存储库层.
c# model-view-controller asp.net-mvc repository domainservices
我想上传一个文件并将其发送到服务层保存,但是我一直在寻找的控制器如何获取HTTPPostedFileBase例子,在控制器中直接将其保存.我的服务层在web dll上没有依赖关系,因此我需要将我的对象读入内存流/字节吗?关于我应该怎么做的任何指示非常感谢...
注意:文件可以通过pdf,word,所以我可能还需要检查内容类型(可能在域服务层...
码:
public ActionResult UploadFile(string filename, HttpPostedFileBase thefile)
{
//what do I do here...?
}
Run Code Online (Sandbox Code Playgroud)
编辑:
public interface ISomethingService
{
void AddFileToDisk(string loggedonuserid, int fileid, UploadedFile newupload);
}
public class UploadedFile
{
public string Filename { get; set; }
public Stream TheFile { get; set; }
public string ContentType { get; set; }
}
public class SomethingService : ISomethingService
{
public AddFileToDisk(string loggedonuserid, int fileid, UploadedFile newupload)
{
var path = @"c:\somewhere";
//if image
Image _image = Image.FromStream(file); …Run Code Online (Sandbox Code Playgroud) 我可以解析文档并生成输出,但是由于ap标记,输出无法解析为XElement,字符串中的所有其他内容都被正确解析.
我的意见:
var input = "<p> Not sure why is is null for some wierd reason!<br><br>I have implemented the auto save feature, but does it really work after 100s?<br></p> <p> <i>Autosave?? </i> </p> <p>we are talking...</p><p></p><hr><p><br class=\"GENTICS_ephemera\"></p>";
Run Code Online (Sandbox Code Playgroud)
我的代码:
public static XElement CleanupHtml(string input)
{
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.OptionOutputAsXml = true;
//htmlDoc.OptionWriteEmptyNodes = true;
//htmlDoc.OptionAutoCloseOnEnd = true;
htmlDoc.OptionFixNestedTags = true;
htmlDoc.LoadHtml(input);
// ParseErrors is an ArrayList containing any errors from the Load statement
if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() …Run Code Online (Sandbox Code Playgroud) c# ×7
asp.net-mvc ×2
.net ×1
.net-3.5 ×1
html-parsing ×1
iqueryable ×1
javascript ×1
jquery ×1
knockout.js ×1
linq-to-sql ×1
nhibernate ×1
repository ×1
resharper ×1