.net core mvc 发布有很长时间了,但是一直没有用过,最近突然想开发一个导航网站,于是就抽时间开发了一个专门为开发者使用的导航站点,想看的话请移步我的上一篇博客
这个网站虽然小但是网站该有的功能它都有。如果你想做一个小的网站,看这个帖子足够了

1 登录过滤器设置
新建一个类,继承 actionfilterattribute重写onactionexecuting方法

 public class loginfilter :actionfilterattribute
    {
       
      
        public filterlogin()
        {
           
        }
        public override void onactionexecuting(actionexecutingcontext context)
        {
            base.onactionexecuting(context);

        //登录逻辑
        //----
        //如果没有登录
         context.result = new statuscoderesult(401);
           
        }
    }
    //控制器里使用
     [httppost]
     [filterlogin()]
        public iactionresult getuser()
        {
        }

坑:这里一定注意要设置context.result不然还会继续执行控制器里的方法

2 全局异常日志设置
nuget引用log4net
配置log4net.config文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- this section contains the log4net configuration settings -->
  <log4net>
    <appender name="consoleappender" type="log4net.appender.consoleappender">
      <layout type="log4net.layout.patternlayout" value="%date [%thread] %-5level %logger - %message%newline" />
    </appender>
    <appender name="rollinglogfileappender" type="log4net.appender.rollingfileappender">
      <file value="logfile/" />
      <appendtofile value="true" />
      <rollingstyle value="composite" />
      <staticlogfilename value="false" />
      <datepattern value="yyyymmdd'.log'" />
      <maxsizerollbackups value="10" />
      <maximumfilesize value="1mb" />
      <layout type="log4net.layout.patternlayout">
        <conversionpattern value="%date [%thread] %-5level %logger [%property{ndc}] - %message%newline" />
      </layout>
    </appender>

    <!-- setup the root category, add the appenders and set the default level -->
    <root>
      <level value="all" />
      <appender-ref ref="consoleappender" />
      <appender-ref ref="fileappender" />
      <appender-ref ref="rollinglogfileappender" />
    </root>
  </log4net>
</configuration>

在startup里添加代码

public static iloggerrepository repository { get; set; }
        public startup(iconfiguration configuration)
        {
            configuration = configuration;
            repository=logmanager.createrepository("netcorerepository");
            xmlconfigurator.configure(repository, new fileinfo("log4net.config"));
        }
          public void configureservices(iservicecollection services)
        {
              services.addmvc(options => { options.filters.add<funclass.errorfilter>(); })
        }

新建类errorfilter

 public class errorfilter : iexceptionfilter
    {
        private ilog log = logmanager.getlogger(startup.repository.name, typeof(httpglobalexceptionfilter));
        public void onexception(exceptioncontext context)
        {
            log.error(context.exception);
        }

    }

3 缓存imemorycache使用
在startup里添加代码

 services.addmemorycache();

在控制器里使用

 private imemorycache _cache;
        public homecontroller(imemorycache cache)
        {
           
            _cache = cache;
          
            
        }

4 session使用
在statup.cs里添加代码

public void configureservices(iservicecollection services)
        {
            services.addsession();
        }
 public void configure(iapplicationbuilder app, ihostingenvironment env)
        {
            app.usesession();
        }

坑:这里必须在services.addmvc()之后。

5 手动获取di对象
有时候我们需要手动获取注入的对象,比如在过滤器里要使用缓存可以这样写

imemorycache _cache = (imemorycache)context.httpcontext.requestservices.getservice(typeof(imemorycache));

还有第二种方法
新建类serviceloader

  public class serviceloader
    {
        public static iserviceprovider instance { get; set; }
    }

在startup.cs 添加代码

 public void configure(iapplicationbuilder app, ihostingenvironment env)
        {
        serviceloader.instance = app.applicationservices;
        }

使用

(imemorycache)funclass.servicelocator.instance.getservice(typeof(imemorycache));

完结散花。
如果大家喜欢的话,别忘点了个站,下篇博客,我将把一个网站怎么从购买域名,备案,域名解析,发布部署.net core mvc站点的过程及遇到的坑讲一遍。