#

Gelöst: in Asp Core

 

Problem:

bei mehrfachen Endpoints in einem API Controller kommt die URL Routing nie an.

Das liegt daran, dass die veralteten Code Beispiele auf Route oder automatisierte mapRoute setzen.

 

Lösung:

Die Lösung liegt im http-Attribut

Man kann das httpGet Attribut mit dem Pfad bzw. der Action erweitern.

 [HttpGet("Action1/{id?}")]

 

 

 

Gilt für:
Asp Core .Net 5,.Net 3, .Net3.1 mit MVC, Razor, Blazor, React und Angular

 

 

        [HttpGet("Read/{id?}")]

        public async Task<IActionResult> Read(long id)

        {

            Console.WriteLine("Images " + Request.Path);

            return Content("Read");

        }

 

 

 

 

 

        [HttpGet("Images/{id}")]

        public async Task<IActionResult> Images(long id)

        {

            Console.WriteLine("Images " + Request.Path);

            return Content("Images");

        }

 

 

 

using Microsoft.AspNetCore.Http;

using Microsoft.AspNetCore.Mvc;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

 

namespace Routes01.Controllers

{

    //[Route("api/[controller]")]

    [Route("api/values")]

    [ApiController]

    public class ValuesController : ControllerBase

    {

        [HttpGet("Read/{id?}")]

        public async Task<IActionResult> Read(long id)

        {

            Console.WriteLine("Images " + Request.Path);

            return Content("Read");

        }

 

 

        [HttpGet("Images/{id}")]

        public async Task<IActionResult> Images(long id)

        {

            Console.WriteLine("Images " + Request.Path);

            return Content("Images");

        }

 

    }

}

 

 

Und in Startup reicht ganz einfach der Asp Routing

 

            app.UseSpaStaticFiles();

 

            app.UseRouting();

 

            app.UseEndpoints(endpoints =>

            {

                endpoints.MapControllerRoute(

                    name: "default",

                    pattern: "{controller}/{action=Index}/{id?}");

            });

 

            app.UseSpa(spa =>

            {

                spa.Options.SourcePath = "ClientApp";

 

                if (env.IsDevelopment())

                {

                    spa.UseReactDevelopmentServer(npmscript: "start");

                }

            });

 

Mobile

.

0123movie.net