Results 1 to 8 of 8

Thread: [RESOLVED] redirect httpget

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,226

    Resolved [RESOLVED] redirect httpget

    Hi.
    Struggling for an hour.
    I have 2 httpGet

    Code:
        [HttpGet]     
        public IsAlive IsAlive()
            {
    and

    Code:
       [HttpGet]
       [Route("api/[controller]/IsAlive2")]
       public IsAlive IsAlive2([FromUri]int id, [FromBody]IsAlive model)
    
    ...
    or
       [HttpGet]
       [Route("api/Vconnect/IsAlive2")]
    public IsAlive IsAlive2([FromUri]int id, [FromBody]IsAlive model)
    
    or any other variation I can think of.
    The first get, I can use the Route and everything will work ok. I can rename it to test, john whatever i will just work.
    The second route, whatever I do it will not hit the breakpoint.
    What is the issue?
    Thanks.

    <Error>
    <Message>No HTTP resource was found that matches the request URI 'http://localhost:39466/api/vConnect/IsAlive2'.</Message>
    <MessageDetail>No action was found on the controller 'VConnect' that matches the name 'IsAlive2'.</MessageDetail>
    </Error>

    P.S. Webapiconfig is set like so:

    Code:
     config.MapHttpAttributeRoutes();
    
      config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional }
    Last edited by sapator; Apr 4th, 2022 at 07:26 AM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,226

    Re: redirect httpget

    Even if pass an optional the @#$#@@$##$ sht MVC will need the id.
    So I need to do this?

    Code:
      [Route("api/Vconnect/IsAlive2")]
            [HttpGet]
            public IsAlive IsAlive2([FromUri] int? id , [FromBody]IsAlive model)
    And send it like:
    http://localhost:39466/api/vconnect/Isalive2?id=4
    or do this:
    public IsAlive IsAlive2([FromUri] int? id , [FromBody]IsAlive model = null) -- that does not seem to work..Anyhow.
    Last edited by sapator; Apr 4th, 2022 at 08:12 AM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,897

    Re: [RESOLVED] redirect httpget

    Quote Originally Posted by sapator View Post
    Hi.
    Struggling for an hour.
    I have 2 httpGet

    Code:
        [HttpGet]     
        public IsAlive IsAlive()
            {
    and

    Code:
       [HttpGet]
       [Route("api/[controller]/IsAlive2")]
       public IsAlive IsAlive2([FromUri]int id, [FromBody]IsAlive model)
    
    ...
    or
       [HttpGet]
       [Route("api/Vconnect/IsAlive2")]
    public IsAlive IsAlive2([FromUri]int id, [FromBody]IsAlive model)
    
    or any other variation I can think of.
    The first get, I can use the Route and everything will work ok. I can rename it to test, john whatever i will just work.
    The second route, whatever I do it will not hit the breakpoint.
    What is the issue?
    Thanks.

    <Error>
    <Message>No HTTP resource was found that matches the request URI 'http://localhost:39466/api/vConnect/IsAlive2'.</Message>
    <MessageDetail>No action was found on the controller 'VConnect' that matches the name 'IsAlive2'.</MessageDetail>
    </Error>

    P.S. Webapiconfig is set like so:

    Code:
     config.MapHttpAttributeRoutes();
    
      config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional }
    Does a route template of
    Code:
     routeTemplate: "api/{controller}/{action}/{id?}",
    work?

  4. #4

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,226

    Re: [RESOLVED] redirect httpget

    That actually breaks the code with:
    The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.
    Parameter name: routeUrl

    It kinda works with a hard coded optional param like : "api/Vconnect/IsAlive2/{id:int=0}"
    So, meah.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,897

    Re: [RESOLVED] redirect httpget

    Quote Originally Posted by sapator View Post
    That actually breaks the code with:
    The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.
    Parameter name: routeUrl

    It kinda works with a hard coded optional param like : "api/Vconnect/IsAlive2/{id:int=0}"
    So, meah.
    I have always tended towards attribute routing when I need to do anything even vaguely useful. The convention based routes are fine for simple things, such as the default routes, but never seem good enough when you need to do something sensible.

  6. #6

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,226

    Re: [RESOLVED] redirect httpget

    It's a simple HttpGet that check something if opted and return an OK or Error. I wouldn't go about doing much to it but I will check attribute routing if I'm able as missing from the office and getting back after a long time has pack a pile of Accounting dept papers in my desk higher than mount Everest.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,897

    Re: [RESOLVED] redirect httpget

    Quote Originally Posted by sapator View Post
    It's a simple HttpGet that check something if opted and return an OK or Error. I wouldn't go about doing much to it but I will check attribute routing if I'm able as missing from the office and getting back after a long time has pack a pile of Accounting dept papers in my desk higher than mount Everest.
    Also, if the route allows a parameter to be optional then the underlying action will require the parameter to either have a default or be nullable.

  8. #8
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    503

    Re: [RESOLVED] redirect httpget

    Quote Originally Posted by PlausiblyDamp View Post
    Also, if the route allows a parameter to be optional then the underlying action will require the parameter to either have a default or be nullable.
    sapator is using VS2015 so some of the new C# and ASPNET features may not exist there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width