Skip to content

Instantly share code, notes, and snippets.

@amilos
Created October 20, 2017 17:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amilos/b8e480f1aa69f78db06499f431fe5732 to your computer and use it in GitHub Desktop.
Save amilos/b8e480f1aa69f78db06499f431fe5732 to your computer and use it in GitHub Desktop.
Use of wildcard and regex based route constraints in asp.net
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using System.IO;
namespace Asseco.Content.Controllers
{
[Route("api/v1/content/")]
public class FolderController : Controller
{
private JsonResult GetFileByPath(string repo, string folder, string file)
{
return new JsonResult(new { action = "download", repo = repo, folder=folder, file=file});
}
private JsonResult ListFolderContentsByPath(string repo, string folder)
{
return new JsonResult(new { action = "traverse", repo = repo, folder=folder});
}
[HttpGet("{repo}/{*path}")]
public JsonResult Get(string repo, string path)
{
var file = Path.GetFileName(path);
var folder = Path.GetDirectoryName(path);
folder = folder.Replace(@"\", @"/").Insert(0, @"/");
//If there is no extension at the end, assume last path segment is a folder
if (string.IsNullOrEmpty(Path.GetExtension(path)))
{
folder += string.Concat(@"/", file);
file = "";
}
//If there is a file in path download file, otherwise list folder contents
if (!string.IsNullOrEmpty(file))
{
return GetFileByPath(repo, folder, file);
}
else
{
return ListFolderContentsByPath(repo, folder);
}
}
[HttpGet("{repo}/{*path:regex(^(.*)/search$)}")]
public JsonResult SearchFolderContentsByPath(string repo, string path)
{
var folder = path.Replace("/search","");
return new JsonResult(new { action = "search", repo = repo, folder = folder});
}
[HttpGet("{repo}/{*path:regex(^(.*)/metadata$)}")]
public JsonResult GetFolderMetadataByPath(string repo, string path)
{
var folder = path.Replace("/metadata","");
return new JsonResult(new { action = "metadata", repo = repo, folder = folder});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment