标签: asp.net-mvc-5

Razor 2到Razor 3 MVC 5

我一直在研究MVC 4解决方案,我一直在尝试将其升级到MVC 5.我已按照此处列出的步骤进行操作.

我已经关注它,现在每当我运行MVC应用程序时,它都会给我这个错误消息:

[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to 
[B]System.Web.WebPages.Razor.Configuration.HostSection. 

Type A originates from 
'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 
'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_2.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. 
Type B originates from 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 
'C:\Users\User\AppData\Local\Temp\Temporary ASP.NET 
Files\root\665ac028\de53a189\assembly\dl3\c2c0a4b5\56e8099e_40e0ce01\System.Web.WebPages.Razor.dll'.
Run Code Online (Sandbox Code Playgroud)

有谁知道这可能是怎么产生的?或者如何解决?到目前为止我环顾四周?我试过更改web.config文件,但没有用...

c# asp.net-mvc razor asp.net-mvc-4 asp.net-mvc-5

54
推荐指数
1
解决办法
4万
查看次数

如何在ASP.NET标识中添加声明

我试图找到一个文档或示例,说明如何使用ASP.NET标识在MVC 5中向用户标识添加自定义声明.该示例应显示在OWIN安全管道中插入声明的位置以及如何使用表单身份验证将它们保存在cookie中.

asp.net-mvc-5 asp.net-identity

53
推荐指数
3
解决办法
7万
查看次数

尝试解析标记帮助程序指令'@addTagHelper'时遇到意外错误

我正在使用Visual Studio 2015社区版,我已经创建了一个ASP.NET MVC 5项目.

当我打开(视图IndexHome或任何其他),它显示了前三行的页面的强调用红色的语法问题.这是错误:

尝试使用值'Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper,Microsoft.AspNet.Mvc.Razor'解析标记帮助程序指令'@addTagHelper'时遇到意外错误.错误:对象引用未设置为对象的实例

截图:

@addTagHelper错误消息

当我构建项目时,它成功构建.当我运行它时,它会显示很多错误,但它会运行应用程序.

索引视图有错误

命名空间"Microsoft.AspNet"中不存在类型或命名空间名称"Mvc"(您是否缺少程序集引用?)

'_Page_views_home_index_cshtml.ExecuteAsync()':找不到合适的方法来覆盖

我怎么能摆脱这个?

c# asp.net-mvc razor asp.net-mvc-5 visual-studio-2015

53
推荐指数
1
解决办法
8159
查看次数

使用JSON.NET返回ActionResult

我正在尝试编写一个C#方法,它将序列化一个模型并返回一个JSON结果.这是我的代码:

    public ActionResult Read([DataSourceRequest] DataSourceRequest request)
    {
        var items = db.Words.Take(1).ToList();
        JsonSerializerSettings jsSettings = new JsonSerializerSettings();
        jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        var converted = JsonConvert.SerializeObject(items, null, jsSettings);
        return Json(converted, JsonRequestBehavior.AllowGet);
    }
Run Code Online (Sandbox Code Playgroud)

当我转到Chrome中的Words/Read时,我得到了以下JSON结果:

"[{\"WordId\":1,\"Rank\":1,\"PartOfSpeech\":\"article\",\"Image\":\"Upload/29/1/Capture1.PNG\",\"FrequencyNumber\":\"22038615\",\"Article\":null,\"ClarificationText\":null,\"WordName\":\"the | article\",\"MasterId\":0,\"SoundFileUrl\":\"/UploadSound/7fd752a6-97ef-4a99-b324-a160295b8ac4/1/sixty_vocab_click_button.mp3\",\"LangId\":1,\"CatId\":null,\"IsActive\":false}
Run Code Online (Sandbox Code Playgroud)

我认为\'转义引号是双重序列化对象时出现的问题.从其他问题: WCF JSON输出添加了不需要的引号和反斜杠

它看起来好像是我对我的对象进行双重序列化,因为我首先使用JSON.NET进行序列化,然后将我的结果传递给Json()函数.我需要手动序列化以避免引用循环,但我认为我的View需要一个ActionResult.

我怎样才能在这里返回一个ActionResult?我需要,还是只能返回一个字符串?

c# serialization json.net asp.net-mvc-5

50
推荐指数
2
解决办法
6万
查看次数

ASP.NET MVC 5文化路由和网址

我翻译了我的mvc网站,这个网站运行得很好.如果我选择其他语言(荷兰语或英语),内容将被翻译.这是有效的,因为我在会话中设置了文化.

现在我想在网址中显示所选文化(=文化).如果它是默认语言,则不应在网址中显示,只有当它不是默认语言时才应在网址中显示.

例如:

对于默认文化(荷兰语):

site.com/foo
site.com/foo/bar
site.com/foo/bar/5
Run Code Online (Sandbox Code Playgroud)

对于非默认文化(英语):

site.com/en/foo
site.com/en/foo/bar
site.com/en/foo/bar/5
Run Code Online (Sandbox Code Playgroud)

我的问题是我总是看到这个:

site.com/ nl/foo/bar/5即使我点击了英文(参见_Layout.cs).我的内容是用英文翻译的,但网址中的路由参数仍然是"nl"而不是"en".

我怎样才能解决这个问题或者我做错了什么?

我尝试在global.asax中设置RouteData但没有帮助.

  public class RouteConfig
  {
    public static void RegisterRoutes(RouteCollection routes)
    {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
      routes.IgnoreRoute("favicon.ico");

      routes.LowercaseUrls = true;

      routes.MapRoute(
        name: "Errors",
        url: "Error/{action}/{code}",
        defaults: new { controller = "Error", action = "Other", code = RouteParameter.Optional }
        );

      routes.MapRoute(
        name: "DefaultWithCulture",
        url: "{culture}/{controller}/{action}/{id}",
        defaults: new { culture = "nl", controller = "Home", action = "Index", id = UrlParameter.Optional },
        constraints: new { culture = "[a-z]{2}" } …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net-mvc asp.net-mvc-routing asp.net-mvc-5

50
推荐指数
2
解决办法
3万
查看次数

实体类型MVC5 EF6中的用户

我在MVC5中创建了一个类,我想要内容的主要所有者,然后我想要一些内容的编辑器:

public class Content
{
    public int ID { get; set; }
    public IdentityUser Owner { get; set; }
    public ICollection<IdentityUser> Editors { get; set; }
    public string Title{ get; set; }
    public string Body { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在数据库上下文中,我有以下代码:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Content>()
        .HasOptional(c => c.Editors)
        .WithRequired()
        .WillCascadeOnDelete();

    modelBuilder.Entity<Content>()
        .HasRequired(c => c.Owner)
        .WithOptional()
        .WillCascadeOnDelete();
}
Run Code Online (Sandbox Code Playgroud)

我希望微软以IdentityUser这种方式实现对象,它可以在其他实体类型中使用,所以我可能做错了,因为当我尝试为Content实体类型创建一个控制器时,我得到以下内容错误:

EntityType 'IdentityUserLogin' has no key defined
EntityType 'IdentityUserRole' has no key defined
EntityType: EntitySet 'IdentityUserLogins' is …
Run Code Online (Sandbox Code Playgroud)

c# asp.net entity-framework asp.net-mvc-5 asp.net-identity

49
推荐指数
2
解决办法
3万
查看次数

font-awesome在MVC5中没有使用bundleconfig

如果我直接参考font-awesome.css_layouts页面.它会工作

<link href="~/Content/font-awesome-4.0.3/css/font-awesome.css" rel="stylesheet" />
Run Code Online (Sandbox Code Playgroud)

但我用过BundleConfig.cs.图标未显示.

 bundles.Add(new StyleBundle("~/Content/css").Include(
                        "~/Content/font-awesome-4.0.3/css/font-awesome.css",
                        "~/Content/bootstrap.css",                        
                         "~/Content/body.css",
                        "~/Content/site.css",
                        "~/Content/form.css"
                     ));
Run Code Online (Sandbox Code Playgroud)

而且我观察到浏览器控制台对字体目录有错误. Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:51130/fonts/fontawesome-webfont.woff?v=4.0.3

可能是什么问题呢?

css fonts twitter-bootstrap font-awesome asp.net-mvc-5

49
推荐指数
2
解决办法
4万
查看次数

ASP.NET标识更改密码

我需要能够通过管理员更改用户的密码.因此,管理员不应输入用户的当前密码,他应该有能力设置新密码.我查看ChangePasswordAsync方法,但此方法需要输入旧密码.因此,此方法不适合此任务.因此我通过以下方式制作:

    [HttpPost]
    public async Task<ActionResult> ChangePassword(ViewModels.Admin.ChangePasswordViewModel model)
    {
        var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        var result = await userManager.RemovePasswordAsync(model.UserId);
        if (result.Succeeded)
        {
            result = await userManager.AddPasswordAsync(model.UserId, model.Password);
            if (result.Succeeded)
            {
                return RedirectToAction("UserList");
            }
            else
            {
                ModelState.AddModelError("", result.Errors.FirstOrDefault());
            }
        }
        else
        {
            ModelState.AddModelError("", result.Errors.FirstOrDefault());
        }
        return View(model);
    }
Run Code Online (Sandbox Code Playgroud)

它有效,但理论上我们可以在AddPasswordAsync方法上收到错误.因此,旧密码将被删除,但未设置新密码.这不好.有什么方法可以在"一次交易"中做到吗?PS.我看到ResetPasswordAsync方法带有重置令牌,似乎,它更安全(因为不能与用户不稳定的情况)但无论如何,它通过2个动作.

c# reset-password asp.net-mvc-5 asp.net-identity-2

49
推荐指数
6
解决办法
6万
查看次数

如何使用AspNet.Identity使用Asp.Net MVC5 RTM位登录/验证用户?

道歉并提前感谢您提出这个问题!我还是SO的新手.

我一直在使用MVC5,EF6和VS 2013开发Web应用程序.

一旦发布,我花了一些时间升级到RC位.感谢所有伟大的帖子:例如.解耦Microsoft.AspNet.Identity.*并将asp.net MVC从5.0.0-beta2更新到5.0.0-rc1!

在我无限的智慧中,我决定转向@Hao Kung在这里发布的RTM位:我如何能够及早访问即将到来的Asp.Net Identity变化?.我认为当我们最终收到RTM版本时,我会省去麻烦并且不会太落后.

这可能是一场噩梦,或者我只是完全遗漏了某些东西(或两者兼而有之),因为我无法弄清楚曾经使用RC1的基本任务.

虽然我似乎是通过控制器登录用户(Asp.Net Identity RTM版本中的Microsoft.AspNet.Identity.Owin.AuthenticationManager在哪里?)...我的WindowsIdentity始终为空,在我调用SignIn后未经过身份验证.正确填充了user和claimIdentity对象.

这是我调用的操作方法(为了完整性将属性移动到局部变量):

[HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
public virtual async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (ModelState.IsValid) {
        var userManager = new UserManager<EtdsUser>(new UserStore<EtdsUser>(new EtdsContext()));
        var user = userManager.Find(model.UserName, model.Password);
        if (user != null) {
            var authenticationManager = HttpContext.GetOwinContext().Authentication;
            authenticationManager.SignOut(new[] {DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.ExternalBearer});
            var claimsIdentity = await userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
            authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = model.RememberMe}, claimsIdentity);
            return RedirectToLocal(returnUrl);
        }
    }
    ModelState.AddModelError("", "The …
Run Code Online (Sandbox Code Playgroud)

c# owin visual-studio-2013 asp.net-mvc-5 asp.net-identity

47
推荐指数
1
解决办法
8万
查看次数

访问MVC 5中控制器中的Claim值

我在我的应用程序中使用了OWIN身份验证.

登录操作

var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, result.UserFirstName));            
claims.Add(new Claim(ClaimTypes.Sid, result.UserID.ToString()));
var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
Run Code Online (Sandbox Code Playgroud)

我想从不同的操作访问UserName和UserID.如何访问声明中添加的值?

更新 我试过了

var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, result.UserFirstName + " " + result.UserLastName));            
claims.Add(new Claim(ClaimTypes.Sid, result.UserIDNumber.ToString()));
var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
var authenticationManager = Request.GetOwinContext().Authentication;
authenticationManager.SignIn(identity);

var claimsPrincipal = new ClaimsPrincipal(identity);
Thread.CurrentPrincipal = claimsPrincipal;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我可以在快速窗口中查看值.但即使我无法访问该值.如何获得价值?

c# asp.net asp.net-mvc claims asp.net-mvc-5

47
推荐指数
2
解决办法
8万
查看次数