ASP.NETCORE UBUNTU

时间:2020-06-15 17:55:22   收藏:0   阅读:57

一个空的asp.net core的网站,从头开始,自己添加各种需要的组件,搭建一个api服务部署到ubuntu上。ubuntu上安装.netcore的环境在my tencent ubuntu上有介绍。

使用vs2019创建一个新的空网站项目,使用.netcore 2.2的版本,不要https和docker支持。

完成后再Program.cs中添加.UseUrls("http://*:5000"),这样才能使其发布到ubuntu上后可以使用ip或者域名访问,不然只能使用localhost访问。

然后再根目录下执行dotnet publish,后会在debuger下面生成一个publish的文件夹,将该文夹下面的内容全部拷贝到ubuntu服务上,然后在目录下执行dotnet 项目.dll就成功运行起来了。默认端口是5000。

这时候使用你的ip加上5000端口就能看到一个返回Hello World!的页面。

 

使用mvc:

Startup.cs中加入mvc的引用:

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(); // 注册MVC到Container。new
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseMvc();//使用mvc中间件,之后我们还要添加Controller来返回数据/资源等等。new
}
}

然后项目下新建一个Controllers文件夹,新建一个TestController类:

namespace WebApplication1.Controllers
{
[Route("[controller]")]
public class TestController : Controller
{
public IActionResult Index()
{
return Ok("this is a test.");
}
}
}

然后启动项目,就可以访问localhost:5000/test就会返回this is a test字符的一个页面。也可以发布到ubuntu上运行效果一致。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!