#1 2016-08-11 13:51:54

talexone
Member
Registered: 2013-08-21
Posts: 21

Custom content from a MVC Application without a template file

Hi,

Can't figure out how to send a custom content (json or text string) from a MVC Application (ex. 30 - MVC Server)

I have declared my custom function in MVCViewModel.pas:

IBlogApplication = interface(IMVCApplication)
    ['{73B27C06-9DB9-45A2-BEDD-2013CFB609D0}']
    function Version: TServiceCustomAnswer;
    ...


function TBlogApplication.Version:TServiceCustomAnswer;
begin
  Result.Header := TEXT_CONTENT_TYPE_HEADER;
  Result.Content := FormatUTF8('Blog Version v1.0',[]);
  Result.Status := HTML_SUCCESS;
end;

But when I call "blog/Version" it ask me to fill Version.html template.


Any help is welcome.
Thanks

Alexandre

Last edited by talexone (2016-08-11 16:28:44)

Offline

#2 2016-08-11 14:03:42

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,237
Website

Re: Custom content from a MVC Application without a template file

This is not how it works.
TServiceCustomAnswer is only for SOA interfaces, not for MVC interfaces.
Under MVC, it has another use, which is to perform redirection between pages.

You should use a method-based service instead, to send any custom content from the app.

Offline

#3 2016-08-11 14:46:09

talexone
Member
Registered: 2013-08-21
Posts: 21

Re: Custom content from a MVC Application without a template file

@ab: Thank you for your answer.

But if it's not to much to ask you to point it in mormot documentation or in samples.

Finally I have found a solution as you said with a method-based service:

type
  TMyServer = class(TSQLRestServerDB)
    published
      procedure Version(Ctxt: TSQLRestServerURIContext);
  end;
  
var aServer: TDeskScreen_Server;
...
procedure TMyServer.Version(Ctxt: TSQLRestServerURIContext);
begin
  Ctxt.Returns(FormatUTF8('Blog Version v1.0',[]),HTTP_SUCCESS,TEXT_CONTENT_TYPE_HEADER);
end;

...
aServer := TMyServer.Create(aModel,ChangeFileExt(ExeVersion.ProgramFileName,'.db'));

Thank you again.

Last edited by talexone (2016-08-11 18:12:13)

Offline

#4 2018-10-21 16:52:55

array81
Member
From: Italy
Registered: 2010-07-23
Posts: 411

Re: Custom content from a MVC Application without a template file

In this case is there a way to know if a user is logged?

I'd like avoid that "Version" return the result if user is not logged in MVC application.

Is it possible? How?

Offline

#5 2018-10-22 06:25:08

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,237
Website

Re: Custom content from a MVC Application without a template file

You have everything you need in the Ctxt: TSQLRestServerURIContext parameter.

Offline

Board footer

Powered by FluxBB