1. ATK Gimmy Susan
  2. SnapObjects
  3. Tuesday, 11 June 2019 19:15 PM UTC
Hi Guru

I'm playing with PB2019 and in particular I'm giving the tutorial: 'https://www.appeon.com/support/documents/appeon_online_help/powerbuilder/CRUD_Operations_with_ModelStore/index.html'

In particular I am trying to rename the controller from 'SampleController' to 'Controller'
(of course I also change the 2 internal references to the class).

If I rename it, it doesn't work (see image). if I leave the default name it works.
What am I forgetting?


ty for answers




>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// controller class
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PowerBuilder.Data;
using Cs1Tris.Servizi;
using System.Linq;

namespace Cs1Tris.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
  public class Controllore : ControllerBase
  {
      private readonly IDepartmentServizio _service;
    
    public Controllore(IDepartmentServizio Service)
    {
        _service = Service;
    }

        // GET api/Sample/Retrieve
    [HttpGet]
    [ProducesResponseType(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    public ActionResult Retrieve()
    {
        IDataStore ds_Department = new DataStore("dw_department");

      try
      {
          ds_Department = _service.Retrieve();    
      }
      catch (Exception ex)
      {
          return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
      }

            if (ds_Department.RowCount <= 0)
      {
          return NotFound("No rows retrieved.");
            }
      else
      {
          return Ok(ds_Department);
      }
        }

    // GET api/sample/load
    [HttpGet]
    public ActionResult> Load()
    {
            return new string[] { "value1", "value2" };
    }
    }
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Accepted Answer
Michael Kramer Accepted Answer Pending Moderation
  1. Tuesday, 11 June 2019 22:57 PM UTC
  2. SnapObjects
  3. # Permalink

Hey Gimmy,

Let me supplement Govinda's detailed replies.

ASP.NET Core follows a set of naming conventions that reduce code volume tremendously. But these conventions are easy to break accidentally - especially when you are new to the platform.

Appeon published several good examples on GitHub that you can simply read to get a better feel for how web API apps are built and their naming conventions. And, you can download and start playing with the code as well.

SnapDevelop's Start Page has link to .NET DataStore Example. Download button below it jumps into GitHub. You will see example using multiple controllers, multiple services, multiple data classes.

A non-PowerBuilder intro written by Microsoft is this one:

https://docs.microsoft.com/en-us/learn/modules/build-web-api-net-core/?view=aspnetcore-2.2

HTH /Michael

 

Comment
  1. ATK Gimmy Susan
  2. Wednesday, 12 June 2019 06:55 AM UTC
thanks a lot

I realize I am a noob



G.
  1. Helpful
  1. Michael Kramer
  2. Wednesday, 12 June 2019 12:05 PM UTC
Happy to help.

And a link into Microsoft's fundamentals intro:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-2.1&tabs=windows

Lots of info. Too much to learn everything upfront. But good link to start learning.

/Michael
  1. Helpful
  1. Michael Kramer
  2. Thursday, 13 June 2019 08:04 AM UTC


Hey Gimmy - and everyone else new to C# though experienced in PowerScript.

Remember as you read C# :: C# is case sensitive - which means the following identifiers are completely different seen in C# code though totally identical seen in PowerScript code:

d_customers -- d_Customers -- D_Customers -- D_Customers -- D_CUSTOMERS -- d_cUstOmErs



And in C# you have namespaces which in PowerScript would be like referencing class names by PBLibrary. Say in PB I have n_calculator in Utility.PBL and later in library list same class name in Science.PBL. If PowerScript had namespaces, it would be like:

lnv_calc = create Science.n_calculator



In PowerScript, multiple classes identically named of identical type across multiple .PBL files => First in Library List wins.

HTH /Michael
  1. Helpful
There are no comments made yet.
Govinda Lopez @Appeon Accepted Answer Pending Moderation
  1. Tuesday, 11 June 2019 19:58 PM UTC
  2. SnapObjects
  3. # 1

Hi Gimmy,

 

Try using a different name that includes the word "Controller" in it. For example name it DepartmentsController. Then, call it with the Route you have specified on the class. For example: http://localhost:5000/api/departments/load.

 

By the way, as a good practice for naming your controllers, always use plural names instead of singular names. For example: use Departments instead of Department.

 

I hope this helps!

 

 

Regards,

Comment
  1. ATK Gimmy Susan
  2. Tuesday, 11 June 2019 21:04 PM UTC
Hi Govinda



Thanks for the reply.



Only now I see that there is an inaccuracy in my message that may have misled you.



I'm trying to rename the class in 'Controllore' not in 'controller'. ( It was a problem with google translator).

I assume that 'controller' is a reserved word.

=)







However, if i rename it to 'ControlloreController' , it does not work.

It seems that I also need to change some other settings, but I don't know where.



Moreover:

1) - Why did you tell me to call with the link 'http://localhost:5000/api/departments/load' and not 'http://localhost:5000/api/sample/load' ?

The tutorial suggest the 2nd link not the one you entered.





2) - where do I find the settings to decide the link of the WEB API ? (http://localhost:5000/api/sample/load' )







P.S.: Sorry if the questions are trivial, but I don't know neither C # nor ASP. I'm learning



P.P.S.: I will treasure your indication of names.



  1. Helpful
  1. ATK Gimmy Susan
  2. Wednesday, 12 June 2019 08:14 AM UTC
Maybe there are ....



You are suggesting that the controller name should be 'xxxxxxController.cs' and callable with 'xxxxxx'.

Alternatively, if the name does not end with 'Controller' I must indicate all the name.

Right?
  1. Helpful
There are no comments made yet.
Govinda Lopez @Appeon Accepted Answer Pending Moderation
  1. Tuesday, 11 June 2019 21:52 PM UTC
  2. SnapObjects
  3. # 2

I see what your problem seems to be:

You have a typo near the end. Change this:

[HttpGet]
public ActionResult> Load()
{
 

for this:

[HttpGet]
public ActionResult< IList < string > > Load()
{

 

It seems that you are missing some code there. 

 

And, answering to your questions:

 

1) - Why did you tell me to call with the link 'http://localhost:5000/api/departments/load' and not 'http://localhost:5000/api/sample/load' ?

A.- It was just an example I was trying to illustrate. The best practice is to name the controller with plural and using the word Controller along the way. So, if you have a Controller that will handle the operations related to the users, a good idea would be to name it: UsersController.

 

2) - where do I find the settings to decide the link of the WEB API ? (http://localhost:5000/api/sample/load' )

A.- That is done with the Route Attribute. You may find it right after declaring the namespace. For example:

namespace Cs1Tris.Controllers
{
     [Route("api/[controller]/[action]")]
     [ApiController]
   public class Controllore : ControllerBase
   {

So, if you change the values from within the Route attribute, then you can define the route of your Web API Controller. In this sense, and referring to your example, you would change the word [controller] with Controllore and the word [action] with Load. The end result would be http://localhost:5000/api/Controllore/Load

 

I hope this helps you understand it better.

 

 

Regards,

Comment
  1. ATK Gimmy Susan
  2. Wednesday, 12 June 2019 06:53 AM UTC


Hi Govinda.

Great suggestion.



I replaced '[Route ("api/[controller]/[action]")]' with '[Route ("api/Gimmy/[action]")]' and the browser responds to 'http://localhost:5000/api/Gimmy/load'

=)



One more question (I hope it's the last)

In '[Route ("api/[controller]/[action]")]' with what is the word '[controller]' replaced ?



thank you



Your Padawan
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.