Copied to clipboard

Flag this post as spam?

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


  • David W. 159 posts 284 karma points c-trib
    Mar 18, 2014 @ 08:06
    David W.
    0

    Umbraco and an external web.api

    We have this web.api-project currently residing in api.mydomain.com. It is as a backend for a mobile app and also a superadminlike javascript application. We also have this new umbraco site (in the same VS-solution) wich currently holds just your plain information (with pages like "about us", "get the app" etc) on www.mydomain.com

    We've setup a asp.net membership across theese domains and are now looking to find a way to have the umbraco site host pages like login, edit profile, start subscription etc. The controller(s) for theese views exist in the api-project.

    I am not sure what aproach to take. I would preferably be able to use the viewmodels that already exists in the sulution and leverage the whole Modelstate/validation-thing.

    We could make our edit profile-forms just to POST to the api-project (CORS is a no go), but then the api project would have to the hackish Request.CreateResponse in order to redirect the user to some hardcoded place back on the umbraco site, right?

    Thoughts?

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 18, 2014 @ 10:20
    Sebastiaan Janssen
    0

    What version of Umbraco is this?

    It could be very simple: take the logic out of your Web API Controllers and make both your API controllers and new SurfaceController use them (so there's one place where the magic happens and your controllers just use those methods.

    For a simple SurfaceController example have a look here (and ignore the webforms part): http://umbraco.com/follow-us/blog-archive/2013/7/14/moving-from-webforms-to-mvc.aspx

  • David W. 159 posts 284 karma points c-trib
    Mar 18, 2014 @ 10:44
    David W.
    0

    Thanks.

    Hm, wouldn't that mean that we need to incorporate and configure our Unity (and other structures) in our umbraco-project? Im not the backend guy on this project but pretty sure we are looking to avoid that. 

    I will have a chat with the backend people though, perhaps they have some thoughts. Maybe something basic such as Logic.PutModel(model) would work...

    (Using Umbraco 7.0.1)

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 18, 2014 @ 11:19
    Sebastiaan Janssen
    0

    I'm not sure where Unity came from..? No, this is all default Umbraco stuff.

    All I'm saying is if you have:

    public class SearchController : UmbracoApiController
    {
        public List<IPublishedContent> PostSearch(string query)
        {
            var results = new List<IPublishedContent>();
    
            if (!string.IsNullOrEmpty(query))
            {
                var helper = new UmbracoHelper(UmbracoContext);
                results.AddRange(helper.TypedSearch(query));
            }
    
            return results;
        }
    }
    

    You could extract the logic to something like:

    internal List<IPublishedContent> GetSearchResults(string query)
    {
        var results = new List<IPublishedContent>();
    
        if (!string.IsNullOrEmpty(query))
        {
            var helper = new UmbracoHelper(UmbracoContext);
            results.AddRange(helper.TypedSearch(query));
        }
        return results;
    }
    

    And use default Umbraco Surface Controllers to do form interactions, while executing the same logic in your API Controllers, for example:

    public class SearchController : ApiController
    {
        public List<IPublishedContent> PostSearch(string query)
        {
            var results = GetSearchResults(query);
    
            return results;
        }
    }
    
    public class SearchController2 : SurfaceController
    {
        public ActionResult PostSearch(string query)
        {
            if (ModelState.IsValid == false)
                return CurrentUmbracoPage();
    
            var results = GetSearchResults(query);
    
            return PartialView("Search", results);
        }
    }
    

    Instead of using strings you'd use your existing models of course.

  • David W. 159 posts 284 karma points c-trib
    Mar 18, 2014 @ 11:34
    David W.
    0

    The Unity-stuff is part of the other projects in this solution (including the web-api).

    I think I understand what you mean. Will look in to it with the team.

     

  • David W. 159 posts 284 karma points c-trib
    Mar 18, 2014 @ 13:32
    David W.
    0

    From what I can gather from the backend guys, we would have to do Unity-hookups in umbraco in order for this suggested solution to work. Having tried doing that previously without success, they are unlikly to try it again I'm afraid. One other suggested solution is to make webapi-calls from a SurfaceController using HttpClient.

    Are we missing something here?

    /D

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 18, 2014 @ 13:34
    Sebastiaan Janssen
    0

    Alright, I have no idea what your setup looks like so I wouldn't be able to comment on that.

    You could try and see if Ajax.BeginForm would work?

  • David W. 159 posts 284 karma points c-trib
    Mar 18, 2014 @ 13:49
    David W.
    0

    The ajax.beginform I assume won't work due to the cross-domain issue. Can give it a shot though. Posting some of the web.api-controller and unityconfig in case it would help you understand their issue:

     

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 18, 2014 @ 13:58
    Sebastiaan Janssen
    0

    Ah, okay, here I was assuming that the Umbraco site and the Web API stuff was living in the same solution, guess I was wrong there. Then I'm out of options, sorry. :-)

  • David W. 159 posts 284 karma points c-trib
    Mar 18, 2014 @ 14:53
    David W.
    0

    The umbraco site and the web api is in the same VS-solution, but are hosted on diffrent domains. ie www.mysite.com and api.mysite.com.

    /D

  • David W. 159 posts 284 karma points c-trib
    Mar 19, 2014 @ 17:51
    David W.
    0

    Just to let you know, we managed to integrate Unity in the umbraco-project this time (with a litlle help from this thread: http://stackoverflow.com/questions/21835555/umbraco-mvc-with-castle-windsor). So it's not really an issue anymore. Thnaks.

Please Sign in or register to post replies

Write your reply to:

Draft