81 lines
1.6 KiB
Markdown
81 lines
1.6 KiB
Markdown
CompressR is a set of nuget packages that help you implement compression on your MVC, and WebApi Actions
|
|
|
|
`install-package CompressR.MVC5`
|
|
|
|
```csharp
|
|
|
|
public class HomeController : Controller
|
|
{
|
|
[Compress]
|
|
public ActionResult Index()
|
|
{
|
|
ViewBag.Title = "Home Page";
|
|
|
|
return View();
|
|
}
|
|
[Gzip]
|
|
public ActionResult About()
|
|
{
|
|
|
|
|
|
return View();
|
|
}
|
|
}
|
|
|
|
```
|
|
|
|
`install-package CompressR.WebApi`
|
|
|
|
```csharp
|
|
|
|
public class ValuesController : ApiController
|
|
{
|
|
[Compress]
|
|
// GET api/values
|
|
public IEnumerable<string> Get()
|
|
{
|
|
return new string[] { "value1", "value2" };
|
|
}
|
|
|
|
// GET api/values/5
|
|
public string Get(int id)
|
|
{
|
|
return "value";
|
|
}
|
|
|
|
// POST api/values
|
|
public void Post([FromBody]string value)
|
|
{
|
|
}
|
|
|
|
// PUT api/values/5
|
|
public void Put(int id, [FromBody]string value)
|
|
{
|
|
}
|
|
|
|
// DELETE api/values/5
|
|
public void Delete(int id)
|
|
{
|
|
}
|
|
}
|
|
|
|
```
|
|
|
|
## Owin
|
|
|
|
You can add gzip support through a middleware. Install the package: `Install-Package CompressR.Owin`. Then add `app.Use<GzipMiddleware>();` to your `Startup.cs`.
|
|
|
|
|
|
```csharp
|
|
|
|
public void Configuration(IAppBuilder app)
|
|
{
|
|
app.Use<GzipMiddleware>();
|
|
app.Run(context =>
|
|
{
|
|
context.Response.ContentType = "text/plain";
|
|
return context.Response.WriteAsync("Hello World!");
|
|
});
|
|
}
|
|
|
|
``` |