我们目前正在将我们的Web表单系统重新开发为Web API和MVC(这对我们来说是新技术)到目前为止,一切似乎都没问题,但我们正在努力将Web API应用程序中的错误发送回MVC应用程序.我们意识到我们需要捕获任何异常,并将这些异常转换为HTTP响应
Web API Product控制器如下所示:
public HttpResponseMessage GetProducts()
{
BAProduct c = new BAProduct();
var d = c.GetProducts();
if (d == null)
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "This is a custom error message");
else
return Request.CreateResponse(HttpStatusCode.OK, d);
}
Run Code Online (Sandbox Code Playgroud)
MVC应用程序将通过以下代码调用Web API: -
public T Get<T>()
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(Config.API_BaseSite);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/myapplicaton/products").Result;
response.EnsureSuccessStatusCode();
T res = response.Content.ReadAsAsync<T>().Result;
return (T)res;
}
}
Run Code Online (Sandbox Code Playgroud)
我们想要实现的是当从MVC应用程序中的Web API收到HTTP错误时,用户被重定向到自定义错误页面,或者在当前视图中显示自定义错误消息(取决于错误).我们遇到的问题是: -
如何访问我们发回的自定义错误消息?(从示例代码中这将是"这是一个自定义错误消息",我们已经通过res中的每个属性并且看不到此消息)
根据状态代码,我们如何捕获并将用户重定向到各个错误页面,即404页面,500页面,并显示已发回的自定义响应消息.我们一直沿着global.asax路线走下去
protected void Application_Error(object sender, EventArgs e)
{ …
Run Code Online (Sandbox Code Playgroud)我们刚刚将textangular升级到1.2.2,因为现在支持拖放.
已经看到textAngualrSetup中的defaultFileDropHandler,但是,努力寻找支持它或如何使用它的任何文档.
defaultFileDropHandler:
/* istanbul ignore next: untestable image processing */
function (file, insertAction)
{
debugger;
var reader = new FileReader();
if(file.type.substring(0, 5) === 'image'){
reader.onload = function() {
if(reader.result !== '') insertAction('insertImage', reader.result, true);
};
reader.readAsDataURL(file);
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
基本上,我们希望允许用户拖动多个pdf,word文档等,并在提交时上传.
我们可以通过在设置页面中的defaultFileDropHandler中添加功能来解决这个问题.
我们实施ta: -
<div text-angular data-ng-model="NoteText" ></div>
Run Code Online (Sandbox Code Playgroud)
但是,有更清洁的方法来实现这一目标吗?
我们目前正在研究一种创建WPF/winforms应用程序的方法,我们可以在内部设置它:
经过多次调查,我们唯一能找到的就是通过以下方式打开网络浏览器: -
object o = null;
SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
IWebBrowserApp wb = (IWebBrowserApp)ie;
wb.Visible = true;
wb.Navigate(url, ref o, ref o, ref o, ref o);
Run Code Online (Sandbox Code Playgroud)
任何建议/阅读建议将不胜感激如何完成该过程.
我们使用AngularJS来设置过滤器.基本上我们有一个开始日期和结束日期和频率.当频率设置为星期时,我们只想在开始日期添加1周,当频率设置为每日时,我们希望将1天添加到开始日期.
基本上是这样的: -
var date = new date();
date.addDays(2);
date.addMonths(2);
date.addYears(2);
Run Code Online (Sandbox Code Playgroud) 希望有人可以帮助解决这个问题.
基本上,我们使用ng-flow https://github.com/flowjs/ng-flow来允许拖放上传项目.我们也在使用MVC 4.
所有似乎都应该工作,但是,我们想定制这个,所以
到目前为止我们尝试了什么?: -
在配置中,我们已禁用目标,以便它不会立即上载
.config(['flowFactoryProvider', function (flowFactoryProvider) {
flowFactoryProvider.defaults = {
target: '',
permanentErrors: [500, 501],
maxChunkRetries: 1,
chunkRetryInterval: 5000,
simultaneousUploads: 1
};
Run Code Online (Sandbox Code Playgroud)
在实际页面上,我们创建了一个按钮,其中ng-click将通过flow.files
<input type="button" value="Click Me" ng-click="uploadme($flow.files)">
Run Code Online (Sandbox Code Playgroud)
在控制器中,我们有函数uploadme,它接受流作为参数.循环遍历此参数并将每个"文件"发布到上传控制器
$scope.uploadme= function (flows)
{
angular.forEach(flows, function (flow) {
var fd = new FormData();
fd.append("file", flow);
$http.post("upload", fd, {
withCredentials: true,
headers: {'Content-Type': undefined },
transformRequest: angular.identity
})
});
}
Run Code Online (Sandbox Code Playgroud)
MVC控制器,'上传'.但是,这个被调用,文件始终为null
public ActionResult upload(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
var fileName = …
Run Code Online (Sandbox Code Playgroud) 正如标题所示,我们正在构建一个新的应用程序,并希望将我们的管理区域设置为nopcommerce所做的新项目.
我们目前正在尝试一个示例项目,到目前为止我们所做的是: -
右键单击解决方案>添加新项目 - 选定的MVC应用程序,并将位置更改为App1应用程序下的新Admin文件夹.这一切现在看起来和nopCommerce一样.
右键单击Admin项目,在build下,将输出更改为..\bin \
据我所见,这似乎是这样吗?
我重建了解决方案,并检查应用程序中的bin目录,管理站点中的dll都在那里.
我发布主站点(App1)并检查.没有创建Admin文件夹,bin目录中没有admin dll ..
我们还缺少哪些其他步骤?
更新:我已经在nopcommerce解决方案中添加了一个新项目,使用完全相同的步骤2和3,这没有任何问题.因此,似乎在webproject或者nopcommerce中的解决方案中都有一些东西正在使这项工作.有任何想法吗?
我在几个代码示例中看到了以下语法,它看起来非常明显,但不是在我看到它的上下文中,所以有人确认
var x = 1
var y = 2
var z = 3
x = y = z
Run Code Online (Sandbox Code Playgroud)
所以在本质上这是否意味着x和y都等于z?
试图谷歌这个,并找不到语法,也看了murach .net书籍没有运气
我刚刚安装了 ReSharper,它已经改变了
if(dto != null)
{
return new test{
obj1 = "",
obj2 = "",
}
}
Run Code Online (Sandbox Code Playgroud)
进入
return dto?.Select(item => new test
{
return new test{
obj1 = "",
obj2 = "",
}
Run Code Online (Sandbox Code Playgroud)
我以前没见过
dto?.Select
Run Code Online (Sandbox Code Playgroud)
试图在没有运气的情况下用谷歌搜索含义..有人可以解释一下,或者为我指出正确的方向
我收集它只是检查空值?
我们对此很陌生,但我们正在尝试使用新的异步datareader
我们一直在关注http://blogs.msdn.com/b/adonet/archive/2012/07/15/using-sqldatareader-s-new-async-methods-in-net-4-5-beta上的示例-Part -2- examples.aspx
但遇到问题.我们要实现的目标是: - 存储过程返回2个表,使用datareader,我们尝试异步填充2个模型,即2个模型将同时创建,而不必等待第一个模型在第二个开始之前建立(如果这有意义?)
以下是我们目前的情况: -
public async Task<UsertResultsModel> GetResults(string user_ID)
{
UsertResultsModel rm = new UsertResultsModel();
List<TableColumn> ltc = new List<TableColumn>();
List<List<TableColumn>> lltc = new List<List<TableColumn>>();
var col = 0;
try
{
using (SqlConnection con = new SqlConnection(Config.DatabaseStringCDA))
{
await con.OpenAsync();
SqlCommand com = new SqlCommand(@"USP_GET_USER_RESULTS", con);
com.CommandType = CommandType.StoredProcedure;
using (SqlDataReader rdr = await com.ExecuteReaderAsync())
{
col = rdr.FieldCount;
while (await rdr.ReadAsync())
{
for (int i = 0; i < rdr.FieldCount; i++) …
Run Code Online (Sandbox Code Playgroud) 有没有办法从lambda表达式中调用函数?
我们目前有以下内容
var obj = m.Select(x => x.ToDictionary(y => y.ColumnName, y => y.ColumnValue))
.ToList();
private static string BuildNewContent(EnumColumnType columnType, String columnValue, List<TableColumn> CurrentRow)
{
}
Run Code Online (Sandbox Code Playgroud)
所以我们真正想做的是将BuildNewContent的返回值作为字典的值添加.如下所示: -
var obj = m.Select(x => x.ToDictionary(y => y.ColumnName, BuildNewContent(y => y.ColumnType, y=> y.ContentValue, y)))
.ToList();
Run Code Online (Sandbox Code Playgroud)
我意识到我们可以通过简单的循环轻松实现这一点,但是,我们希望看到如何使用Lambda(如果可能的话)
更新 - 根据要求,M的类型是TableViewModel,它是一个嵌套列表
public class TableViewModel
{
public List<List<TableColumn>> Grid { get; set; }
}
public class TableColumn
{
public TableColumn() { }
public TableColumn(string columnHeader, string columnValue, int columnWidth, EnumColumnType columnType, string columnName)
{
this.ColumnHeader = …
Run Code Online (Sandbox Code Playgroud) 我们对此很新.最后设法让ui-Bootstrap使用日期控件: -
<div class="col-md-2 box" ng-controller="ResultFilter" >
<h2>Filter</h2>
<p class="input-group" ng-controller="DatepickerDemoCtrl">
<input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="resultfilter.startdate" is-open="opened1" min="minDate" max="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
<span class="input-group-btn">
<button class="btn btn-default" ng-click="open($event, 'opened1')"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
<p class="input-group" ng-controller="DatepickerDemoCtrl">
<input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="resultfilter.enddate" is-open="opened2" min="minDate" max="'2015-06-22'" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
<span class="input-group-btn">
<button class="btn btn-default" ng-click="open($event, 'opened2')"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
<p class="input-group">
<select class="form-control" ng-model="resultfilter.frequency">
<option value="Daily">Daily</option>
<option value="Weekly">Weekly</option>
<option value="Monthly">Monthly</option>
<option value="Yearly">Yearly</option>
</select>
</p>
</div> …
Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×5
angularjs ×4
asp.net-mvc ×4
javascript ×3
.net-4.5 ×1
asp.net ×1
async-await ×1
browser ×1
flow-js ×1
lambda ×1
linq ×1
nopcommerce ×1
textangular ×1
winforms ×1
wpf ×1