我是ASP.Net MVC 4的新手.我正在开发一个拥有大量路由的应用程序.考虑到这一点,我在控制器中遇到命名冲突.因此,我决定将Controller分成多个控制器.为了保持干净,我觉得我需要将Controller类放在Controllers目录中的子目录中.我的问题是:
谢谢!
我正在开发一个使用ASP.NET MVC 4的应用程序.在某些方面,我觉得我从头开始学习一切:).我被告知它值得.
我需要在控制器中将一些JSON发布到Action.我的行动如下:
[Authorize]
public class MyController : Controller
{
[HttpPost]
public ActionResult RemoveItem(string itemID)
{
// Do stuff...
return Json(new { Status = 1, Message="Success" });
}
}
Run Code Online (Sandbox Code Playgroud)
我的JQuery代码如下所示:
function removeItem(id) {
var json = { "itemID": id };
$.ajax({
type: "POST",
url: "/myController/removeItem",
contentType: "application/json; charset=utf-8",
data: json,
dataType: "json",
success: removeItemCompleted,
error: removeItemFailed
});
}
function removeItemCompleted(results) {
}
function removeItemFailed(request, status, error) {
}
Run Code Online (Sandbox Code Playgroud)
在Fiddler中,我注意到返回了500错误.响应中的TITLE字段说:"无效的JSON原语:itemID".
我究竟做错了什么?
谢谢!
我继承了一个 C# (.NET 2.0) 应用程序,它有一堆静态方法。我需要将这些方法之一转换为基于异步事件的方法。当方法完成时,我想触发一个事件处理程序。我的问题是,我可以从静态方法中触发事件处理程序吗?如果是这样,如何?
当我谷歌时,我只找到 IAsyncResult 示例。但是,我希望能够执行以下操作:
EventHandler myEvent_Completed;
public void DoStuffAsync()
{
// Asynchrously do stuff that may take a while
if (myEvent_Completed != null)
myEvent_Completed(this, EventArgs.Empty);
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
这看起来应该非常简单.但是,对于我的生活,我似乎无法在ASP.NET MVC中创建一个cookie.目前,我有以下代码:
DateTime lastActivityDate = DateTime.UtcNow;
if (Request.Browser.Cookies)
{
HttpCookie lastActivityCookie = new HttpCookie(COOKIE_LAST_ACTIVITY, lastActivityDate.ToShortDateString());
lastActivityCookie.Expires = DateTime.Now.AddMonths(-12);
this.ControllerContext.HttpContext.Response.Cookies.Add(lastActivityCookie);
}
Run Code Online (Sandbox Code Playgroud)
我设置了一个断点,注意到cookie似乎被添加了.(是的,我进入了Request.Browser.Cookies街区).然后我尝试使用以下方法检索cookie:
DateTime lastActivity = DateTime.UtcNow.AddDays(-7); // Default to the past week
HttpCookie lastActivityCookie = Request.Cookies[COOKIE_LAST_ACTIVITY];
if (lastActivityCookie != null)
{
DateTime temp = DateTime.UtcNow;
if (String.IsNullOrWhiteSpace(lastActivityCookie.Value) == false)
{
if (DateTime.TryParse(lastActivityCookie.Value, out temp))
lastActivity = temp;
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,lastActivityCookie永远null.此外,当我查看Chrome中的"资源"选项卡时,我会看到cookie分支,但是,我正在尝试创建的cookie未列出.但是列出了另外两个cookie,包括.ASPXAUTH cookie.我究竟做错了什么?
我试图在ASP.NET MVC 4应用程序中设置一个登录表单.目前,我已经配置了我的视图,如下所示:
RouteConfig.cs
routes.MapRoute(
"DesktopLogin",
"{controller}/account/login",
new { controller = "My", action = "Login" }
);
Run Code Online (Sandbox Code Playgroud)
MyController.cs
public ActionResult Login()
{
return View("~/Views/Account/Login.cshtml");
}
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model)
{
return View("~/Views/Account/Login.cshtml");
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在浏览器中访问/ account/login时,收到错误消息:
The current request for action 'Login' on controller type 'MyController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Login() on type MyApp.Web.Controllers.MyController
System.Web.Mvc.ActionResult Login(MyApp.Web.Models.LoginModel) on type MyApp.Web.Controllers.MyController
Run Code Online (Sandbox Code Playgroud)
如何在ASP.NET MVC 4中设置基本表单?我已经看过ASP.NET MVC 4中的示例Internet App模板.但是,我似乎无法弄清楚路由是如何连接的.非常感谢你的帮助.
我有一个SQL Azure数据库.我可以通过Azure管理门户成功连接到数据库.另外,我可以使用Entity Framework从我的应用程序成功连接到数据库.不幸的是,我无法通过SQL Server Management Studio连接到数据库.
我的想法是SQL Server Management Studio无法连接到"主"数据库.出于这个原因,它会失败.因此,我尝试通过"连接属性"选项卡上的"连接到数据库"字段添加数据库名称.然而,我得到了同样的错误.该错误如下所示:
TITLE: Connect to Server
------------------------------
Cannot connect to tcp:{serverName}.database.windows.net,1433.
------------------------------
ADDITIONAL INFORMATION:
Login failed for user '{username}'.
This session has been assigned a tracing ID of '{id}'. Provide this tracing ID to customer support when you need assistance. (Microsoft SQL Server, Error: 18456)
For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&EvtSrc=MSSQLServer&EvtID=18456&LinkId=20476
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?这个"服务器"上有2个SQL Azure数据库,我想通过管理工作室访问它.如何添加用户以便我可以登录管理这两个数据库?有没有办法在SQL Azure数据库上管理用户?我似乎无法弄明白.
谢谢您的帮助.