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: Asp.Net Core Controller löst ID nicht auf

26.02.2021 (👁16493)


Problem:

Bei Asp.Net Core kommt im Controller bei einer HttpPut und HttpPost Methode nie die /id ID der aufrufenden URL an


const response = await fetch('api/articles/33' , requestOptions); //*SEND DATA

const data = await response.json();




 

Im Asp.Net Core Controller sieht man, dass die id als Erster Parameter hier mit int IDArticle nicht gefunden oder nicht aufgelöst wird aus der URL heraus

 

 

Lösung:

 

Man MUSS den ersten Parameter mit id benennen, wie in dem HttpPut Argument [HttpPut("{id}")]

// PUT: api/tbl_Articles/5 ==============

        [HttpPut("{id}")]

        //[ValidateAntiForgeryToken]

        //[Authorize]

        public async Task<ActionResult<ApiArticleModel>> PutArticle(int id, ApiArticleModel apiArticle)

        {

            int IDArticle = id;

            var test = Request.Path;

            // -------------< Edit_Postback() > -------------

            if (IDArticle != apiArticle.IDArticle)

            {

                return BadRequest();

            }

 

Alternativ kann man auch in dem Controller Argument den Filterbegriff anpassen

        // PUT: api/tbl_Articles/5 ==============

        [HttpPut("{IDArticle}")]

        //[ValidateAntiForgeryToken]

        //[Authorize]

        public async Task<ActionResult<ApiArticleModel>> PutArticle(int IDArticle, ApiArticleModel apiArticle)

        {

            //int IDArticle = id;

            var test = Request.Path;

            // -------------< Edit_Postback() > -------------

            if (IDArticle != apiArticle.IDArticle)

            {

                return BadRequest();

            }

 

Im Debug Mode im Asp.Net Core Controller

 

 

Code Dokumentation:

 

Startup.cs

>      Router Auflösung UseEndpoints in Asp.Net Core 5 für Web API Controller

            app.UseEndpoints(endpoints =>

            {

                endpoints.MapControllerRoute(

                name: "api",

                pattern: "/{controller}/{id?}");

                //endpoints.MapControllerRoute(

                //    name: "default",

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

                endpoints.MapRazorPages();

            });

  

 

Edit.js (React Client Datei)

è Aufruf der PUT Methode zum Speichern von Daten im Asp.Net Core API Controller

Api Aufruf sogar mit fester URL als Controller und ID: 'api/articles/58'

   async send_Data_to_Api() {

        console.log(this.state);

        const requestOptions = {

            method: 'PUT',

            headers: { 'Content-Type': 'application/json' },

            body: JSON.stringify(

                {

                    idarticle: this.state.idarticle,

                    iduser: this.state.iduser,

                    title: this.state.title,

                    textcontent: this.state.textContent,

                    htmlcontent: this.state.htmlContent,

                    folder: this.state.folder,

                    keywords: this.state.keywords,

                   

                })

        };

        const response = await fetch('api/articles/58' , requestOptions); //*SEND DATA

        const data = await response.json();

        this.setState({

            idarticle: data.idArticle,

            iduser: data.idUser,

            title: data.title,

            textcontent: data.textContent,

            htmlcontent: data.htmlContent,

            folder: data.folder,

            keywords: data.keywords,

            nimages: data.nImages,

            nvideos: data.nVideos,

            nfiles: data.nFiles,

            dtcreated: data.dtCreated,

            dtedit: data.dtEdit,

            loading: false,

            status: "data is send"

        });

        alert('Data are send');

    }