2015-06-15 18:57:46 +02:00
|
|
|
unit App1MainControllerU;
|
|
|
|
|
2017-01-18 21:53:53 +01:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2015-06-15 18:57:46 +02:00
|
|
|
interface
|
|
|
|
|
2017-01-18 21:53:53 +01:00
|
|
|
uses
|
|
|
|
MVCFramework,
|
|
|
|
MVCFramework.Commons,
|
2015-06-15 18:57:46 +02:00
|
|
|
MVCFramework.Logger,
|
|
|
|
Web.HTTPApp;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
[MVCPath('/')]
|
|
|
|
TApp1MainController = class(TMVCController)
|
|
|
|
public
|
|
|
|
[MVCPath('/')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2021-02-03 18:23:48 +01:00
|
|
|
procedure Index;
|
2015-06-15 18:57:46 +02:00
|
|
|
|
|
|
|
[MVCPath('/hello')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2019-01-18 19:35:37 +01:00
|
|
|
procedure HelloWorld;
|
2015-06-15 18:57:46 +02:00
|
|
|
|
|
|
|
[MVCPath('/hello')]
|
|
|
|
[MVCHTTPMethod([httpPOST])]
|
2019-01-18 19:35:37 +01:00
|
|
|
procedure HelloWorldPost;
|
2015-06-15 18:57:46 +02:00
|
|
|
|
|
|
|
[MVCPath('/div/($par1)/($par2)')]
|
|
|
|
[MVCHTTPMethod([httpGET])]
|
2019-01-18 19:35:37 +01:00
|
|
|
procedure RaiseException;
|
2015-06-15 18:57:46 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2019-01-18 19:35:37 +01:00
|
|
|
JsonDataObjects,
|
|
|
|
MVCFramework.Serializer.JsonDataObjects,
|
2015-06-15 18:57:46 +02:00
|
|
|
System.SysUtils;
|
|
|
|
|
|
|
|
{ TApp1MainController }
|
|
|
|
|
2019-01-18 19:35:37 +01:00
|
|
|
procedure TApp1MainController.HelloWorld;
|
2015-06-15 18:57:46 +02:00
|
|
|
begin
|
|
|
|
Render('Hello World called with GET');
|
2019-01-18 19:35:37 +01:00
|
|
|
if Context.Request.ThereIsRequestBody then
|
|
|
|
Log('Body:' + Context.Request.Body);
|
2015-06-15 18:57:46 +02:00
|
|
|
end;
|
|
|
|
|
2019-01-18 19:35:37 +01:00
|
|
|
procedure TApp1MainController.HelloWorldPost;
|
2015-06-15 18:57:46 +02:00
|
|
|
var
|
|
|
|
JSON: TJSONObject;
|
2019-01-18 19:35:37 +01:00
|
|
|
lSer: TMVCJsonDataObjectsSerializer;
|
2015-06-15 18:57:46 +02:00
|
|
|
begin
|
2019-01-18 19:35:37 +01:00
|
|
|
lSer := TMVCJsonDataObjectsSerializer.Create;
|
2017-03-30 21:22:54 +02:00
|
|
|
try
|
2019-01-18 19:35:37 +01:00
|
|
|
JSON := lSer.ParseObject(Context.Request.Body);
|
|
|
|
try
|
|
|
|
JSON.S['modified'] := 'from server';
|
2021-02-03 18:23:48 +01:00
|
|
|
Render(JSON, False);
|
2019-01-18 19:35:37 +01:00
|
|
|
Log('Hello world called with POST');
|
|
|
|
finally
|
|
|
|
JSON.Free;
|
|
|
|
end;
|
2017-03-30 21:22:54 +02:00
|
|
|
finally
|
2019-01-18 19:35:37 +01:00
|
|
|
lSer.Free;
|
2017-03-30 21:22:54 +02:00
|
|
|
end;
|
2015-06-15 18:57:46 +02:00
|
|
|
end;
|
|
|
|
|
2021-02-03 18:23:48 +01:00
|
|
|
procedure TApp1MainController.Index;
|
2015-06-15 18:57:46 +02:00
|
|
|
begin
|
|
|
|
Redirect('index.html');
|
|
|
|
end;
|
|
|
|
|
2019-01-18 19:35:37 +01:00
|
|
|
procedure TApp1MainController.RaiseException;
|
2015-06-15 18:57:46 +02:00
|
|
|
var
|
|
|
|
R: Extended;
|
2019-01-18 19:35:37 +01:00
|
|
|
lJSON: TJSONObject;
|
2015-06-15 18:57:46 +02:00
|
|
|
begin
|
2019-01-18 19:35:37 +01:00
|
|
|
Log('Parameter1=' + Context.Request.Params['par1'].QuotedString);
|
|
|
|
Log('Parameter2=' + Context.Request.Params['par2'].QuotedString);
|
2021-02-03 18:23:48 +01:00
|
|
|
R := StrToInt(Context.Request.Params['par1']) / StrToInt(Context.Request.Params['par2']);
|
2019-01-18 19:35:37 +01:00
|
|
|
lJSON := TJSONObject.Create;
|
|
|
|
lJSON.F['result'] := R;
|
|
|
|
Render(lJSON);
|
2015-06-15 18:57:46 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|