aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.61k stars 2.14k forks source link

inherit controller problem in mvc 6 beta-6 #2891

Closed yukozh closed 9 years ago

yukozh commented 9 years ago

I created a base controller class and inherits from Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Mvc.Rendering;
using System.Security.Claims;
using CodeComb.OA.Models;

namespace CodeComb.OA.Controllers
{
    public class BaseController : Controller
    {
        [FromServices]
        protected UserManager<User> UserManager { get; private set; }

        [FromServices]
        protected SignInManager<User> SignInManager { get; private set; }

        [FromServices]
        protected RoleManager<User> RoleManager { get; private set; }

        protected readonly OAContext DB = new OAContext();

        protected User CurrentUser{ get; private set; }

        protected BaseController()
        {
            if (User.IsSignedIn())
            {
                var uid = int.Parse(User.GetUserId());
                try
                {
                    CurrentUser = UserManager.Users.Where(x => x.Id == uid).Single();
                }
                catch
                {
                    SignInManager.SignOutAsync().Wait();
                }
            }
            else
            {
                CurrentUser = null;
            }
        }
    }
}

Then, I made HomeController, AccountController,... inherits it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Authentication;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Mvc.Rendering;
using System.Security.Claims;
using CodeComb.OA.Models;

// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace CodeComb.OA.Controllers
{
    public class AccountController : BaseController
    {
        [HttpGet]
        [AllowAnonymous]
        public async Task<ActionResult> SendCode(string returnUrl = null, bool rememberMe = false)
        {
            var user = await SignInManager.GetTwoFactorAuthenticationUserAsync();
            if (user == null)
            {
                return View("Error");
            }
            var userFactors = await UserManager.GetValidTwoFactorProvidersAsync(user);
            var factorOptions = userFactors.Select(purpose => new SelectListItem { Text = purpose, Value = purpose }).ToList();
            return View(new SendCodeViewModel { Providers = factorOptions, ReturnUrl = returnUrl, RememberMe = rememberMe });
        }

I visit http://localhost:xxxx/Account/Login returns 404 error.

the router not works.

but in MVC5 it works.

yukozh commented 9 years ago

Sorry for my English