1

Right now we have a lot of dummy MVC controllers that return simple views with web components (vuejs). I'm trying to refactor this to see if we can use the controllerless razor plugin but I don't want to execute the actual service logic when the route is requested by the browser because the component already does this. An example:

/account/edit/1 is a standard MVC controller -> is associated to a view that has something like this <account edit="1" /> that makes an ajax call to /api/account/get/1 . I want to add the Service Stack Razor engine so that we can just use:

/api/account/get/1 (we would remove the api part or add the alternate route) and this would not execute the logic but would execute the security attributes so we can remove the standard MVC controllers that aren't be used. I know we can add different services for this to get it to work but I would prefer to use the same endpoint and just not execute it but return the razor.

Any ideas?

Not the answer I wanted to hear but essentially we can't do what we want, instead we'll create some dummy service stack services to return the correct views which still eliminates the controllers.

lucuma
  • 18,247
  • 4
  • 66
  • 91

1 Answers1

2

In order to use ServiceStack.Razor's No Ceremony Option where you can return dynamic Razor Views without needing a ServiceStack Service your route needs to match up with the razor page so you wouldn't have a route like:

/account/get/1

As that would require a Razor Content page at:

/account/get/1.cshtml

You can instead add it to the queryString like /account/get?id=1 as then you can have a Razor Content page like:

/account/get?id=1

If you wanted to handle /account/get/1 you'd need a Service that handles that route, e.g:

[Route("/account/get/{Id}", "GET")]
class ViewAccount 
{
    public int Id { get; set; }
}

Your Service can just return the same Request DTO, e.g:

public object Get(ViewAccount request) => request;

Which will be convention be able to handle a Razor View located at:

/Views/ViewAccount.cshtml

With the model:

@model ViewAccount

Note: It's generally not recommended to have /get in routes, this is normally differentiated by a HTTP GET request so I'd recommend either removing the get and just having:

[Route("/account/{Id}", "GET")]

Or if you wanted a separate route for Razor views use something other than a HTTP Verb which can be confusing, IMO view is more appropriate for HTML pages, e.g:

[Route("/account/view/{Id}", "GET")]
mythz
  • 141,670
  • 29
  • 246
  • 390
  • I'm not sure this answers my question. I just used get as an example. Let's say the routes are setup and pointing to the correct service that gets the account for that id. Will the logic of the service be called prior to the razor being generated? This is what I'm trying to avoid as all of our views have components like mini spa apps that will be calling itself in effect to get the data. – lucuma Jun 02 '17 at 17:15
  • @lucuma if they're pointing to a Service it always gets executed before the view. The Razor View is a [HTML Content Type representation](http://docs.servicestack.net/architecture-overview) of the Service Response, but the No Ceremony option lets you bypass requiring a Service and executing the Razor Page directly as long as the page matches the route. Also make sure you know the difference between [Content Pages vs View Pages](https://stackoverflow.com/a/13206221/85785) – mythz Jun 02 '17 at 17:20
  • @lucuma If you're building a SPA you normally don't want HTML views and just want to access the JSON responses by Ajax, if you use a JSON client ServiceStack will automatically return JSON response of the Service. – mythz Jun 02 '17 at 17:21
  • It isn't a true spa. We just have all of the front end functionality as vuejs components which are like angular directives. This makes all the MVC controllers bare and adds duplicate work for security etc that I'm trying to get rid off. – lucuma Jun 02 '17 at 17:24
  • @lucuma The Security/Validation needs to be implemented on the back-end always and only optionally added on the client to improve UX. Also checkout docs on [API First Development](https://github.com/ServiceStackApps/EmailContacts#api-first-development) as it can simplify Validation on the client, even using ajax requests. – mythz Jun 02 '17 at 17:27
  • All of our services have security and validation. All of the MVC controllers have security too. Trying to remove these dummy MVC controllers. I'll check out the no ceremony option. – lucuma Jun 02 '17 at 17:30