Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lars Rasmussen 5 posts 45 karma points
    Jan 30, 2023 @ 19:06
    Lars Rasmussen
    0

    How to remove /Umbraco/API/ from Url

    Hi

    I have a Umbraco API Controller "Products" and a API "Rates".

    public class ProductsController : UmbracoApiController

    public JObject Rates()

    It gives me a Url "~/Umbraco/API/Products/Rates" how can I remove the "/Umbraco/API/" from the Url so it looks like "~/Products/Rates"??

    How you can help :-)

  • Garðar Þorsteinsson 114 posts 536 karma points
    Jan 30, 2023 @ 19:46
    Garðar Þorsteinsson
    0

    Hi Lars,

    You can Rewrite the urls by adding this to your web.config inside of system.webServer

    It will rewrite the url from ~/Umbraco/API/Products/Rates to ~/API/Products/Rates

    I would not recommend to remove the api completely, but you can by changing.

    <match url="^api/(.*)" /> 
    

    to

    <match url="^/(.*)" /> 
    

    This code also rewrites it for the surface controllers.

    <rewrite>
      <rules>
        <rule name="Umbraco/api -&gt; /api" enabled="true" stopProcessing="true">
          <match url="^api/(.*)" />
          <action type="Rewrite" url="umbraco/api/{R:1}" />
        </rule>
        <rule name="Umbraco/surface -&gt; / surface" enabled="true" stopProcessing="true">
          <match url="^surface/(.*)" />
          <action type="Rewrite" url="umbraco/surface/{R:1}" />
        </rule>
      </rules>
    </rewrite>
    
  • Johannes Lantz 156 posts 838 karma points c-trib
    Jan 30, 2023 @ 21:37
    Johannes Lantz
    101

    Hi Lars!

    An alternative would be to use "Attribute routing". Then you could do something like this

    public class AttributeRoutingComponent :IComponent
    {
        public void Initialize()
        {
            GlobalConfiguration.Configuration.MapHttpAttributeRoutes();        
    
        }
    
        public void Terminate()
        {
    
        }
    }
    
    public class AttributeRoutingComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
           composition.Components().Append<AttributeRoutingComponent>(); ;
        }
    }
    
    public class ProductsController : UmbracoApiController
    {
        [Route("products/rates")]
        public string Rates()
        {
            //Do stuff
        }
    }
    

    Then you could access it via products/rates. You can read more about it here

    Hope this helps you get on the right track!

    //Johannes

  • Garðar Þorsteinsson 114 posts 536 karma points
    Jan 30, 2023 @ 22:08
    Garðar Þorsteinsson
    0

    I agree with Johannes, if you have a new set up then Attribute routing would be the way to go.

    My recommendation only works for Version 8 and lower.

Please Sign in or register to post replies

Write your reply to:

Draft