Asp.net core 3.1 中的路由

Posted

技术标签:

【中文标题】Asp.net core 3.1 中的路由【英文标题】:Routing in Asp.net core 3.1 【发布时间】:2021-01-29 05:49:33 【问题描述】:

我是 .Net Core 的新手,我想要一个 MVC 应用程序,它允许我的用户通过 GUI 界面。这部分很简单并且可以工作,但是我想添加某些控制器以由某种类型的 powershell 调用。我想创建一个 ApiController 并调用 Web 请求,这会起作用,但是它返回的网页没有找到。

Startup.cs

        public void ConfigureServices(IServiceCollection services)
        

            services.AddTransient<ISdgCEPublishDbServices, SdgCEPublishDbServices>();
            services.AddTransient<ISdgTpServices, SdgTpServices>();
            services.AddControllers();
            services.AddControllersWithViews();
        

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        
            if (env.IsDevelopment())
            
                app.UseDeveloperExceptionPage();
            
            else
            
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "controller=Home/action=Index/id?");
            );
        

这是我的控制器


    [ApiController]
    [Route("api/[controller]")]
    public class PublishsController : ControllerBase
    
        private readonly ISdgCEPublishDbServices sdgCEPublishDbServices;
        private readonly ISdgTpServices sdgTpServices;

        public PublishsController(ISdgCEPublishDbServices _sdgCEPublishDbServices, ISdgTpServices _sdgTpServices) 
        
            sdgCEPublishDbServices = _sdgCEPublishDbServices;
            sdgTpServices = _sdgTpServices;
        

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("/Pblsh")]
        public async Task<bool> publish()
        
            try
            
                var sdTp = await sdgTpServices.getByLikeDesc("CE");
                return true;
            
            catch(Exception ex)
            
                throw new Exception(ex.Message);
            
        
...

我尝试使用这些呼叫进行呼叫,但它一直说找不到

https://localhost:44374/api/Publishs/Pblsh https://localhost:44374/Publishs/Pblsh

【问题讨论】:

尝试 [Route("Pblsh")] 而不是 [Route("/Pblsh")]。第一个网址应该可以工作 是的,[Route] 属性中的值与其父级(控制器)相对 谢谢你,我猜想了。如果你想写一个答案,不接受它作为答案,或者我可以删除线程。 【参考方案1】:

尝试[Route("Pblsh")] 而不是[Route("/Pblsh")]

这个网址应该可以工作:https://localhost:44374/api/Publishs/Pblsh

旁注:你也可以这样做:[HttpGet("Pblsh")]

【讨论】:

【参考方案2】:

您可以检查 Properties 文件夹和 launchSetting.Json 文件。您可以在此处定义应用程序启动时的路由。

launchUrl

"profiles": "IIS Express": "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "api/Publishs", "environmentVariables": "ASPNETCORE_ENVIRONMENT": "Development" , "MyProject": "commandName": "Project", "launchBrowser": true, "launchUrl": "api/Publishs", "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": "ASPNETCORE_ENVIRONMENT": "Development"

【讨论】:

以上是关于Asp.net core 3.1 中的路由的主要内容,如果未能解决你的问题,请参考以下文章