Jseb
Jseb

Reputation: 1934

Routing in Asp.net core 3.1

I am new to .Net Core and I want an MVC applications which will allow my users to go through the GUI interaface. This part is easy and it works, but I want to add certain controller to be called by powershell of some sort. I figure create an ApiController and call the web request and this would work, however it return webpage was not found.

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?}");
            });
        }

This is my controller


    [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);
            }
        }
...

I try calling using these call but it keeps saying not found

Upvotes: 0

Views: 277

Answers (2)

You can check the Properties folder and launchSetting.Json file. There you can define the route of your application on application start.

  • 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" } } }

Upvotes: 0

maxc137
maxc137

Reputation: 2781

Try [Route("Pblsh")] instead of [Route("/Pblsh")].

This url should work: https://localhost:44374/api/Publishs/Pblsh

Side note: you also can do it like this: [HttpGet("Pblsh")]

Upvotes: 1

Related Questions