Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Gelöst: Route kommt in Asp Core Net5 nicht an

11.03.2021 (👁1678)

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

                }

            });