我试图将一个服务注入我的动作过滤器,但我没有在构造函数中注入所需的服务.这是我有的:
public class EnsureUserLoggedIn : ActionFilterAttribute
{
private readonly ISessionService _sessionService;
public EnsureUserLoggedIn()
{
// I was unable able to remove the default ctor
// because of compilation error while using the
// attribute in my controller
}
public EnsureUserLoggedIn(ISessionService sessionService)
{
_sessionService = sessionService;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
// Problem: _sessionService is null here
if (_sessionService.LoggedInUser == null)
{
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
context.Result = new JsonResult("Unauthorized");
}
}
}
Run Code Online (Sandbox Code Playgroud)
而我正在装饰我的控制器:
[Route("api/issues"), EnsureUserLoggedIn]
public class IssueController : …Run Code Online (Sandbox Code Playgroud) 我对C#中的自动属性有点困惑,例如
public string Forename{ get; set; }
Run Code Online (Sandbox Code Playgroud)
我知道你不需要声明私有变量来保存代码,但是当你不使用任何get或set逻辑时,属性的重点是什么?为什么不用
public string Forename;
Run Code Online (Sandbox Code Playgroud)
我不确定这两个语句之间有什么区别,如果你想要额外的get/set逻辑,我一直认为你使用过属性?
我有一个使用Session的ASP.Net vNext项目.但是我在尝试获取/设置会话中的值时收到此错误.
Microsoft.AspNet.Http.Core.dll中出现"System.InvalidOperationException"类型的异常,但未在用户代码中处理
附加信息:尚未为此应用程序或请求配置会话.
这是我的控制器方法:
[AllowAnonymous]
[HttpGet("/admin")]
public IActionResult Index()
{
if (Context.Session.GetString("UserName") == null) // error thrown here
{
return RedirectToAction("Login");
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
我已经"Microsoft.AspNet.Session": "1.0.0-beta3"在我的project.json文件中添加了KVM包,并将我的应用程序配置为通过我的方式使用会话Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// code removed for brevity
services.AddCachingServices();
services.AddSessionServices();
}
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
app.UseInMemorySession(configure: s => s.IdleTimeout = TimeSpan.FromMinutes(30));
}
Run Code Online (Sandbox Code Playgroud)
我查看了Github上的vNext文档,但它没有提供有关ASP.Net会话的大量信息.我究竟做错了什么?
我正在使用Backbone.js学习JavaScript MVC应用程序开发,并且在视图中渲染模型集合时遇到问题.这就是我想要做的事情:
页面完成加载后,从服务器检索数据作为模型集合
在视图中渲染它们
这就是我想做的一切,这就是我到目前为止所做的一切:
$(function(){
"use strict";
var PostModel = Backbone.Model.extend({});
var PostCollection = Backbone.Collection.extend({
model: PostModel,
url: 'post_action.php'
});
var PostView = Backbone.View.extend({
el: "#posts-editor",
initialize: function(){
this.template = _.template($("#ptpl").html());
this.collection.fetch({data:{fetch:true, type:"post", page:1}});
this.collection.bind('reset', this.render, this);
},
render: function(){
var renderedContent = this.collection.toJSON();
console.log(renderedContent);
$(this.el).html(renderedContent);
return this;
}
});
var postList = new PostCollection();
postList.reset();
var postView = new PostView({
collection: postList
});
});
Run Code Online (Sandbox Code Playgroud)
据我所知,Chrome正在记录来自服务器的响应,并且它是JSON格式,就像我想要的那样.但它并没有在我看来呈现.控制台中没有明显的错误.
服务器有一个处理程序,它接受GET参数并回显一些JSON:
http://localhost/blog/post_action.php?fetch=true&type=post&page=1
[
{
"username":"admin",
"id":"2",
"title":"Second",
"commentable":"0",
"body":"This is the second …Run Code Online (Sandbox Code Playgroud) 我有一个WebAPI应用程序,用于数据库中的一些RESTful操作.它工作得很好,但我希望将路由与根URL匹配.例如,我想转到网站的根目录,看看一些动态生成的有用信息.
目前,我已将其设置为遵循标准惯例api/{controller}/{action},但如何在导航到根目录时显示此信息而不是类似的内容api/diagnostics/all?
基本上我想要的是,当用户导航到根URL时,我想将该请求路由到 TestController.Index()
我在WebApiConfig.cs文件中进行了以下设置:
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "Index",
routeTemplate: "",
defaults: new { controller = "Test", action = "Index" }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional, controller = "Test" }
);
}
Run Code Online (Sandbox Code Playgroud)
这就是我的TestController.cs的样子:
[RoutePrefix("api")]
public class TestController : ApiController
{
[Route("TestService"), HttpGet]
public string Index()
{
return "Service is running normally...";
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的模型
public class Issue
{
public Guid Id { get; set; }
[Required]
public User ReportedByUser { get; set; }
public User ClosedByUser { get; set; }
public Category Category { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行ef migrations add <MigrationName>时,出现以下错误:
实体类型“WebProject.Models.Issue”上的导航“ReportedByUser”尚未添加到模型中,或被忽略,或目标实体类型被忽略。
当User模型中只有 1 个导航属性类型时,我不会收到此错误。我如何使用上面的模型进行这项工作?
我有一个 Ecto 模型,我想将其存储在数据库中,并使用 elixir 函数生成的自定义 binary_id 。这可能吗?
我的 id 函数如下所示:
def gen_id
String.upcase to_string Enum.take_random('abcdefghjkmnpqrstuvwxyz123456789', 8)
end
Run Code Online (Sandbox Code Playgroud)
我的架构如下所示:
schema "orders" do
belongs_to :type, Invoicer.Customer
@primary_key {:id, :binary_id, autogenerate: true}
field :order_details, :string
timestamps()
end
Run Code Online (Sandbox Code Playgroud) c# ×5
.net-core ×2
asp.net ×2
api ×1
asp.net-core ×1
asp.net-mvc ×1
backbone.js ×1
c#-3.0 ×1
ecto ×1
elixir ×1
javascript ×1
json ×1
session ×1