delphimvcframework/samples/iocpsample/App1MainControllerU.pas

75 lines
1.6 KiB
ObjectPascal
Raw Normal View History

2013-10-30 00:46:50 +01:00
unit App1MainControllerU;
interface
2015-04-01 17:01:23 +02:00
uses
MVCFramework,
{$IF CompilerVersion <= 27}
DATA.DBXJSON,
{$ELSE}
System.JSON,
{$ENDIF}
2013-10-30 00:46:50 +01:00
MVCFramework.Logger;
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
2015-04-01 17:01:23 +02:00
DATA.DBXJSON,
2013-10-30 00:46:50 +01:00
System.SysUtils;
{ TApp1MainController }
procedure TApp1MainController.HelloWorld(ctx: TWebContext);
begin
Render('Hello World called with GET');
end;
procedure TApp1MainController.HelloWorldPost(ctx: TWebContext);
var
2015-04-01 17:01:23 +02:00
JSON: TJSONObject;
2013-10-30 00:46:50 +01:00
begin
2015-04-01 17:01:23 +02:00
JSON := ctx.Request.BodyAsJSONObject;
JSON.AddPair('modified', 'from server');
Render(JSON.Clone as TJSONObject);
2013-10-30 00:46:50 +01:00
Log('Hello world called with POST');
end;
procedure TApp1MainController.Index(ctx: TWebContext);
begin
Redirect('/public_html/index.html');
end;
procedure TApp1MainController.RaiseException(ctx: TWebContext);
var
R: Extended;
begin
Log('Parameter1=' + ctx.Request.Params['par1'].QuotedString);
Log('Parameter2=' + ctx.Request.Params['par2'].QuotedString);
2015-04-01 17:01:23 +02:00
R := StrToInt(ctx.Request.Params['par1']) / StrToInt(ctx.Request.Params['par2']);
2013-10-30 00:46:50 +01:00
Render(TJSONObject.Create(TJSONPair.Create('result', TJSONNumber.Create(R))));
end;
end.