Copied to clipboard

Flag this post as spam?

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


  • Craig Willard 21 posts 126 karma points
    Dec 18, 2014 @ 02:51
    Craig Willard
    0

    Razor script accessible by URL?

    I'd like to use razor to create a "page" that would return some HTML based upon parameters passed in via the querystring.  My idea is to call this page via AJAX to load some markup into a separate page.

    So, I'd do an ajax call to something like /my-page.aspx?search=sometext&page=2 and it would basically hit a razor script that would return only the HTML that I output via my custom code, which I'd then place onto that separate page.

    Make sense?  Is this possible?  

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Dec 18, 2014 @ 05:54
    Jan Skovgaard
    100

    Hi Craig

    You can use an alternative template where you call a Razor macro with your logic for listening to the parameters etc.

    An alternative template is just a template, which can be used for displaying a page in an alternative fashion. It does not need to be connected to a document type.

    You can create a template and call it "Proxy" for instance and enter the text "Hi".

    Then you can try calling it like this http://yourdomain.com/Proxy - Then you should see "Hi" written in your browser.

    Now you can create the Razor macro to handle the data you need in your ajax request.

    I hope this makes sense.

    /Jan

  • Craig Willard 21 posts 126 karma points
    Dec 18, 2014 @ 15:40
    Craig Willard
    0

    Jan, this is just what I was looking for.  Easy peasy!

    Thank you!

    --Craig

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Dec 18, 2014 @ 16:36
    Jan Skovgaard
    0

    Hi Craig

    Glad it worked for you :) Actually I just came across Warrens blogpost about GET/POST XML or JSON for scenarios like these where he does not recommend using the alt template. Totally forgot about that one - It might be of inspiration and use as well http://creativewebspecialist.co.uk/2013/07/16/why-i-think-your-doing-it-wrong-umbraco-alttemplate-data-views/

    But the approach above is working as well but might not be as pretty as the one Warren suggests, which is more clean and the better way.

    Happy coding.

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft