我正在尝试创建一个控制台应用程序作为webapi的主机。它运行良好,并为API控制器提供服务。我还希望将一个超文本标记语言文件添加到同一个控制台应用程序中,并使用Microsoft.OWIN.StaticFiles中间件和StartUp.cs中的以下代码对其进行托管。
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}"
);
appBuilder.UseWebApi(config);
string exeFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string webFolder = Path.Combine(exeFolder,"/TestFolder");
appBuilder.UseStaticFiles(webFolder);
}
但是当我尝试使用Url浏览页面时,我得到了错误
未找到与请求URI“”http://localhost:9999/testfolder/samplehtml.html“”.No类型匹配的HTTP资源。“”未找到与名为“”testfolder“”的控制器匹配的HTTP资源。“”
看起来名为“API”的文件夹就像一个TestFolder控制器。有没有可能实现这样的事情,或者我在这里遗漏了什么?
发布于 2015-03-22 20:23:01
我能够解决这个问题。我不得不更改路由模板以添加常量前缀'api‘。因此,新的工艺路线模板变为:
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}"
);
注意'api‘关键字的用法。这区分了我的两个请求,因为系统现在知道,只有当url包含关键字时,它才需要查找API控制器。因此,当我需要发送api请求时,使用
http://localhost:8888/api/sample/getdate
对于html页面,请使用
http://localhost:8888/testfolder/sample.html
https://stackoverflow.com/questions/29193455
复制相似问题