当前位置:首页 > Windows程序 > 正文

" api/{controller}/{action}/{id} "

2024-03-31 Windows程序

标签:

措施集和宿主不在一个措施集

新建一个类库:

SelfHost: 要领一:

1.添加对MyControllers类库的引用.

2.在控制台代码中插手一行代码:

技术分享图片

固然,可以添加多个措施集.(记得引用)

var config = new HttpSelfHostConfiguration(":9527"); config.Routes.MapHttpRoute( "API Default", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional }); //加载外部措施集 Assembly.Load("MyControllers"); using (var server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("请开始您的演出"); Console.ReadLine(); }

View Code

3.测试功效:

要领二:

1.新建自界说的 AssembliesResolver 类

技术分享图片

View Code

2.改削控制台代码:

技术分享图片

var config = new HttpSelfHostConfiguration(":9527"); config.Routes.MapHttpRoute( "API Default", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional }); //加载外部措施集 //Assembly.Load("MyControllers"); //Assembly.Load("TestControllers"); config.Services.WordStr(typeof(IAssembliesResolver), new MyAssembiesResolver()); using (var server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("请开始您的演出"); Console.ReadLine(); }

View Code

实际上就添了一行代码...

3.测试:

上面的要领将措施集写死在代码里,这必定不切合要求.

要领三:

新建3个类,如下:

public class AppConfigSetting { public static string AssembliesLoadFrom => GetValue(); public static string GetValue([CallerMemberName]string key = null) { return System.Configuration.ConfigurationManager.AppSettings[key]; } }

public class AssembliesLoad : ConfigurationSection { [ConfigurationProperty("", IsDefaultCollection = true)] public AssemblyElementCollection AssemblyNames { get { return (AssemblyElementCollection)this[""]; } } public static AssembliesLoad GetSection() { return ConfigurationManager.GetSection(AppConfigSetting.AssembliesLoadFrom) as AssembliesLoad; } } public class AssemblyElementCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new AssemblyElement(); } protected override object GetElementKey(ConfigurationElement element) { AssemblyElement serviceTypeElement = (AssemblyElement)element; return serviceTypeElement.AssemblyName; } } public class AssemblyElement : ConfigurationElement { [ConfigurationProperty("assemblyName", IsRequired = true)] public string AssemblyName { get { return (string)this["assemblyName"]; } set { this["assemblyName"] = value; } } }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/33739.html

Jm-杰米博客Jamie
草根站长的技术交流乐园!IT不会不要紧快来好好学习吧!
  • 20786文章总数
  • 7494587访问次数
  • 建站天数
  • 友情链接