delphimvcframework/samples/basicdemo_server/App1MainControllerU.pas

74 lines
1.5 KiB
ObjectPascal
Raw Normal View History

2013-10-29 16:51:16 +01:00
unit App1MainControllerU;
interface
uses MVCFramework,
MVCFramework.Logger,
Web.HTTPApp;
type
[MVCPath('/')]
TApp1MainController = class(TMVCController)
public
[MVCPath('/')]
[MVCHTTPMethod([httpGET])]
procedure Index;
2013-10-29 16:51:16 +01:00
[MVCPath('/hello')]
[MVCHTTPMethod([httpGET])]
procedure HelloWorld;
2013-10-29 16:51:16 +01:00
[MVCPath('/hello')]
[MVCHTTPMethod([httpPOST])]
procedure HelloWorldPost;
2013-10-29 16:51:16 +01:00
[MVCPath('/div/($par1)/($par2)')]
[MVCHTTPMethod([httpGET])]
procedure RaiseException(par1, par2: String);
2013-10-29 16:51:16 +01:00
end;
implementation
uses
2014-11-19 12:27:37 +01:00
{$IF CompilerVersion >= 27} System.JSON,
2014-05-11 19:29:20 +02:00
{$ELSE} Data.DBXJSON,
{$ENDIF}
2013-10-29 16:51:16 +01:00
System.SysUtils;
{ TApp1MainController }
procedure TApp1MainController.HelloWorld;
2013-10-29 16:51:16 +01:00
begin
Render('Hello World called with GET');
if Context.Request.ThereIsRequestBody then
Log('Body:' + Context.Request.Body);
2013-10-29 16:51:16 +01:00
end;
procedure TApp1MainController.HelloWorldPost;
2013-10-29 16:51:16 +01:00
var
2014-05-11 19:29:20 +02:00
JSON: TJSONObject;
2013-10-29 16:51:16 +01:00
begin
JSON := Context.Request.BodyAsJSONObject;
2014-05-11 19:29:20 +02:00
JSON.AddPair('modified', 'from server');
Render(JSON, false);
2013-10-29 16:51:16 +01:00
Log('Hello world called with POST');
end;
procedure TApp1MainController.Index;
2013-10-29 16:51:16 +01:00
begin
2014-05-11 19:29:20 +02:00
Redirect('index.html');
2013-10-29 16:51:16 +01:00
end;
procedure TApp1MainController.RaiseException(par1, par2: String);
2013-10-29 16:51:16 +01:00
var
R: Extended;
begin
Log('Parameter1=' + QuotedStr(par1));
Log('Parameter2=' + QuotedStr(par2));
R := StrToInt(par1) / StrToInt(par2);
2013-10-29 16:51:16 +01:00
Render(TJSONObject.Create(TJSONPair.Create('result', TJSONNumber.Create(R))));
end;
end.