我有一个MVC网站,其中访问基于各种角色.一旦用户登录系统,他们就可以看到导航到他们被授权的页面.但是,某些用户可能仍尝试使用直接URL访问页面.如果是这样,系统会自动将它们重定向到登录页面.而不是登录页面我想将它们重定向到另一个视图(未授权).
Web.Config具有以下条目:
<customErrors mode="On">
<error statusCode="401" redirect="~/Home/Unauthorized" />
<error statusCode="404" redirect="~/Home/PageNotFound" />
</customErrors>
<authentication mode="Forms">
<forms name="Development" loginUrl="~/Account/Login" cookieless="UseCookies" timeout="120"></forms>
</authentication>
Run Code Online (Sandbox Code Playgroud)
我也在Global.asax.cs中注册了这些路由.
routes.MapRoute(
name: "Unauthorized",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Unauthorized", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "PageNotFound",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "PageNotFound", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
它够了吗?
这是我面临的一个问题,当从C#Entity Framework存储在SQL Server数据库中时会导致精度损失.
decimal(20, 15)
public decimal AssignedGrossBudget { get; set; }
可能有什么不对?(我使用实体框架db.SaveChanges();和SQLBulkCopy将数据从c#存储到SQL Server)
我想存储34.09090909090909而不是34.090000000000000.
我通过直接插入表中检查它是否有效.
我有一种情况,我以下列格式收到日期作为字符串.
"2014年1月13日星期一00:00:00 GMT + 0000(GMT标准时间)"
我需要将其转换为c#中的以下格式(日期/字符串)以进行进一步处理
YYYY-MM-DD (2014-01-13)
Convert.ToDateTime(SelectedData)
Run Code Online (Sandbox Code Playgroud)
以上代码出现以下错误:
'Convert.ToDateTime(SelectedData)' threw an exception
of type 'System.FormatException' System.DateTime {System.FormatException}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我无法更改我收到日期最佳问候的格式.
Kendo UI Web和Kendo UI Core有什么区别
我正在尝试通过 Web API(使用实体框架)下载 excel 文件。下载正在运行,但我在尝试打开文件时收到一些关于文件损坏的错误对话框。
Web API 代码如下:
public HttpResponseMessage GetValue(int ID, string name)
{
MemoryStream stream;
try {
using (DataContext db = new DataContext()) {
dynamic fileObj = (from c in db.FileList c.ID == IDc).ToList();
stream = new MemoryStream(fileObj(0).File);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue(fileObj(0).FileContentType);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = name };
return result;
}
} catch (Exception ex) {
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
Run Code Online (Sandbox Code Playgroud)
它打开带有两个错误对话框和以下消息的文件。
Excel 完成了文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃
我们有一个方法,它将文件夹名称和天数作为参数。
public void Delete(string folder, int days)
{
var files = Directory.GetFiles(folder);
foreach (var file in files)
{
var fi = new FileInfo(file);
var fiCreationTime = fi.CreationTime;
var deleteOlderThan= DateTime.Now.AddDays(-days);
if (fiCreationTime >= deleteOlderThan) continue;
fi.Delete();
}
}
Run Code Online (Sandbox Code Playgroud)
在 C# 中对此类方法进行单元测试的最佳方法是什么
我正在使用Kendo UI Core(免费版),并希望将文件上传到Web Server(通过MVC Controller).我知道付费的Kendo UI版本的方式,但我想用免费版本做到这一点.
见如下
用于Kendo UI上传的HTML
<div class="demo-section k-header">
<input name="files" id="files" type="file" />
</div>
Run Code Online (Sandbox Code Playgroud)
Java脚本
$("#files").kendoUpload({
async: {
saveUrl: "save",
removeUrl: "remove",
autoUpload: true
}
});
Run Code Online (Sandbox Code Playgroud)
它添加了一个按钮,如下所示:
现在,一旦我选择了文件,我想通过MVC Controller将其上传到服务器.
我该如何从这里调用MVC控制器?
干杯
我正在尝试使用Web API与Web窗体应用程序通过Ajax调用获取数据.
因此创建了一个Web API项目并将其部署到本地IIS.它回应了这些要求.
然后在webForm(Default.aspx)中进行Ajax调用以获取数据.
$(document).ready(function () {
var url = 'http://localhost:8080/api/Values';
$.ajax({
url: url,
type: 'POST',
crossdomain: 'true',
contentType: "application/json; charset=utf-8;",
type: 'GET',
success: function (result) {
var r = JSON.stringify(result);
alert("From Web-API " + r);
}
});
});
Run Code Online (Sandbox Code Playgroud)
但这是跟随错误
"XMLHttpRequest无法加载http:// localhost:8080/api/Values.请求的资源上没有'Access-Control-Allow-Origin'标头.因此不允许来源' http:// localhost:61605 '访问. "
我在这里错过哪个标题?
如何在 Kendo UI Treeview Checked 中获取所选项目的文本?
例如alert($(this).data.text); 不管用。我需要将所有选定节点的文本发送到服务器我想在数组中获取此信息。
$("#treeview .k-item input[type=checkbox]:checked").closest(".k-item").each(function () {
// change whatever you want, for example:
**alert($(this).data.text);**
$(this).css("color", "green");
});
Run Code Online (Sandbox Code Playgroud)
谢谢你。哈迪普
c# ×4
jquery ×2
kendo-ui ×2
.net ×1
ajax ×1
asp.net-mvc ×1
date-format ×1
datetime ×1
decimal ×1
excel ×1
html ×1
kendo-upload ×1
moq ×1
sql-server ×1
sqldatatypes ×1
unauthorized ×1
unit-testing ×1