尝试在视图中使用模型时出现NancyFX错误

时间:2017-04-17 13:31:05

标签: c# razor

所以我正在使用NancyFX SelfHost创建一个应用程序并且它运行完美,索引等显示,但是当我进入登录页面时,我得到了一个" Razor编译错误"没有任何错误。我尝试了以下问题的解决方案:Razor Compilation Error using NancyFXYSOD when passing complex type as razor model in NancyFX尝试使用Google搜索并尝试所有可能的解决方案,但它没有用。这是我的login.cshtml:

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<Test.Models.AuthModel>
<html>
<body>
    <h1>Login</h1>
    <form method="POST">
        Username <input type="text" name="Username" />
        <br />
        Password <input name="Password" type="password" />
        <br />
        Remember Me <input name="RememberMe" type="checkbox" value="True" />
        <br />
        <input type="submit" value="Login" />
    </form>
    @if(@Model.Errored)
    {
        <div>Invalid Username or Password</div>
    }
</body>
</html>

Visual Studio说这会产生一些错误:

The name 'Model' does not exist in the current context  

The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?) 

The type or namespace name 'Helpers' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

The type or namespace name 'WebPages' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

'_Page_Views_login_cshtml.Execute()': no suitable method found to override
The name 'Context' does not exist in the current context

The type or namespace name 'Nancy' could not be found (are you missing a using directive or an assembly reference?) 

所有这些错误都在login.cshtml中......

然后是AuthModule.cs:

public class AuthModule : NancyModule
    {
        public AuthModule()
        {
            Get("/login", args =>
            {
                AuthModel model = new AuthModel();
                model.Errored = this.Request.Query.error.HasValue;
                return Negotiate.WithView("login").WithStatusCode(HttpStatusCode.OK).WithModel(model);
            });
            Post("/login", args => {
                var userGuid = UserDatabase.ValidateUser((string)this.Request.Form.Username, (string)this.Request.Form.Password);

                if (userGuid == null)
                {
                    return this.Context.GetRedirect("~/login?error=true&username=" + (string)this.Request.Form.Username);
                }

                DateTime? expiry = null;
                if (this.Request.Form.RememberMe.HasValue)
                {
                    expiry = DateTime.Now.AddDays(7);
                }

                return this.LoginAndRedirect(userGuid.Value, expiry);
            });
        }
    }

BootStrapper.cs:

public class BootStrapper : DefaultNancyBootstrapper
    {
        protected override void ConfigureConventions(NancyConventions nancyConventions)
        {
            base.ConfigureConventions(nancyConventions);

            nancyConventions.StaticContentsConventions.Add(
                StaticContentConventionBuilder.AddDirectory("/assets")
            );
        }

        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);


            container.Register<IRazorConfiguration, RazorConfig>().AsSingleton();
            container.Register<IUserMapper, UserDatabase>();
            container.Register<RazorViewEngine>();
        }

        protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
        {
            base.RequestStartup(container, pipelines, context);
            pipelines.OnError += (ctx, ex) =>
            {
                var msg = ex.Message;
                return null;
            };
            var formsAuthConfiguration = new FormsAuthenticationConfiguration()
            {
                RedirectUrl = "~/login",
                UserMapper = container.Resolve<IUserMapper>(),
            };
            FormsAuthentication.Enable(pipelines, formsAuthConfiguration);
        }
    }

RazorConfig:

public class RazorConfig : IRazorConfiguration
    {
        public IEnumerable<string> GetAssemblyNames()
        {

        }

        public IEnumerable<string> GetDefaultNamespaces()
        {

        }

        public bool AutoIncludeModelNamespace
        {
            get { return true; }
        }
    }

我不知道如何解决这个问题,如果有人能指出我正确的方向,那就太棒了。

由于

0 个答案:

没有答案