NancyFx / Nancy

Lightweight, low-ceremony, framework for building HTTP based services on .Net and Mono
http://nancyfx.org
MIT License
7.16k stars 1.47k forks source link

How to put some codes into HTML <head></head> tags? #2969

Closed akrsnr closed 5 years ago

akrsnr commented 5 years ago

I’m creating form via C# and Nancy and not using extra .html files. It creates html codes via toHTML() method written by me. After obtaining the html code as string, I use return Response.AsText(form.toHtml(), “text/html”);. When I investigate the html code on a browser, the codes are put into <body>..</body> tags. What if I need to put the following?

  <meta charset=“utf-8”>
    <title>test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

How can I put them into <head></head> tags?

grumpydev commented 5 years ago

Nancy will output whatever you tell it to output, so its probably a bug in your toHtml method, you're probably not outputting valid HTML with head tags, so the browser is trying its best to interpret the content and wrapping it in a body tag.

akrsnr commented 5 years ago

Nancy will output whatever you tell it to output, so its probably a bug in your toHtml method, you're probably not outputting valid HTML with head tags, so the browser is trying its best to interpret the content and wrapping it in a body tag.

So, isn’t there any Nancy-way to put my strings into tags other than my toHTML() method?

grumpydev commented 5 years ago

I'm not entirely sure what you're trying to achieve, but what you're doing there is returning an entire html document formed from whatever you toHTML method is returning. If you're wanting to combine that with other html/templates you should have a look at view engines, if you're wanting to update content that's already in the browser via ajax then that's nothing really to do with nancy.