delphimvcframework/samples/BasicDemo/App1MainControllerU.pas

75 lines
1.7 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(ctx: TWebContext);
[MVCPath('/hello')]
[MVCHTTPMethod([httpGET])]
procedure HelloWorld(ctx: TWebContext);
[MVCPath('/hello')]
[MVCHTTPMethod([httpPOST])]
procedure HelloWorldPost(ctx: TWebContext);
[MVCPath('/div/($par1)/($par2)')]
[MVCHTTPMethod([httpGET])]
procedure RaiseException(ctx: TWebContext);
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(ctx: TWebContext);
begin
Render('Hello World called with GET');
2014-09-05 12:47:40 +02:00
if ctx.Request.ThereIsRequestBody then
Log('Body:' + ctx.Request.Body);
2013-10-29 16:51:16 +01:00
end;
procedure TApp1MainController.HelloWorldPost(ctx: TWebContext);
var
2014-05-11 19:29:20 +02:00
JSON: TJSONObject;
2013-10-29 16:51:16 +01:00
begin
2014-05-11 19:29:20 +02:00
JSON := ctx.Request.BodyAsJSONObject;
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(ctx: TWebContext);
begin
2014-05-11 19:29:20 +02:00
Redirect('index.html');
2013-10-29 16:51:16 +01:00
end;
procedure TApp1MainController.RaiseException(ctx: TWebContext);
var
R: Extended;
begin
Log('Parameter1=' + QuotedStr(ctx.Request.Params['par1']));
Log('Parameter2=' + QuotedStr(ctx.Request.Params['par2']));
2014-09-05 12:47:40 +02:00
R := StrToInt(ctx.Request.Params['par1']) /
2013-10-29 16:51:16 +01:00
StrToInt(ctx.Request.Params['par2']);
Render(TJSONObject.Create(TJSONPair.Create('result', TJSONNumber.Create(R))));
end;
end.