本文整理汇总了C#中System.Web.Http.Routing.UrlHelper类的典型用法代码示例。如果您正苦于以下问题:C# UrlHelper类的具体用法?C# UrlHelper怎么用?C# UrlHelper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UrlHelper类属于System.Web.Http.Routing命名空间,在下文中一共展示了UrlHelper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Enrich
private void Enrich(TodoRepresentation todo, UrlHelper url)
{
var selfUrl = url.Link("DefaultApi", new { controller = "todos", todo.id });
todo.AddLink(new SelfLink(selfUrl));
todo.AddLink(new EditLink(selfUrl));
todo.AddLink(new DeleteLink(selfUrl));
}
示例2: Get
public IEnumerable<StudentV2BaseModel> Get(int page = 0, int pageSize = 10)
{
IQueryable<Student> query;
query = TheRepository.GetAllStudentsWithEnrollments().OrderBy(c => c.LastName);
var totalCount = query.Count();
var totalPages = Math.Ceiling((double)totalCount / pageSize);
var urlHelper = new UrlHelper(Request);
var prevLink = page > 0 ? urlHelper.Link("Students", new { page = page - 1, pageSize = pageSize }) : "";
var nextLink = page < totalPages - 1 ? urlHelper.Link("Students", new { page = page + 1, pageSize = pageSize }) : "";
var paginationHeader = new
{
TotalCount = totalCount,
TotalPages = totalPages,
PrevPageLink = prevLink,
NextPageLink = nextLink
};
System.Web.HttpContext.Current.Response.Headers.Add("X-Pagination",
Newtonsoft.Json.JsonConvert.SerializeObject(paginationHeader));
var results = query
.Skip(pageSize * page)
.Take(pageSize)
.ToList()
.Select(s => TheModelFactory.CreateV2Summary(s));
return results;
}
示例3: Get
public object Get(int contactgroupid, int contactId = 0, int page = 0)
{
var userName = _identityService.CurrentUser;
var baseQuery = TheRepository.GetContactGroupContacts(_identityService.CurrentUser,contactgroupid)
.OrderByDescending(c => c.FirstName);
var totalCount = baseQuery.Count();
var totalPages = Math.Ceiling((double)totalCount / PAGE_SIZE);
var helper = new UrlHelper(Request);
var preUrl = page > 0 ? helper.Link("Contacts", new { page = page - 1 }) : "";
var newUrl = page < totalPages - 1 ? helper.Link("Contacts", new { page = page + 1 }) : "";
var results = baseQuery
.Skip(PAGE_SIZE * page)
.Take(PAGE_SIZE)
.ToList()
.Select(c => TheModelFactory.Create(c));
return new
{
TotalCount = totalCount,
TotalPages = totalPages,
PrevPageUrl = preUrl,
NextPageUrl = newUrl,
Results = results,
};
}
示例4: Get
public Object Get(int page = 0, int pageSize = 10)
{
IQueryable<Course> query;
query = TheRepository.GetAllCourses().OrderBy(c => c.CourseSubject.Id);
var totalCount = query.Count();
var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
var urlHelper = new UrlHelper(Request);
var prevLink = page > 0 ? urlHelper.Link("Courses", new { page = page - 1, pageSize = pageSize }) : "";
var nextLink = page < totalPages - 1 ? urlHelper.Link("Courses", new { page = page + 1, pageSize = pageSize }) : "";
var results = query
.Skip(pageSize * page)
.Take(pageSize)
.ToList()
.Select(s => TheModelFactory.Create(s));
return new
{
TotalCount = totalCount,
TotalPages = totalPages,
PrevPageLink = prevLink,
NextPageLink = nextLink,
Results = results
};
}
示例5: SetUp
public void SetUp()
{
var config = new HttpConfiguration();
var request = new HttpRequestMessage(HttpMethod.Post, "http://dragonapi.devjakobsen.dk/api/player");
var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "player" } });
playerController = new PlayerController();
UrlHelper urlHelper = new UrlHelper(request);
playerController.ControllerContext = new HttpControllerContext(config, routeData, request);
playerController.Request = request;
playerController.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
playerController.Request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
playerController.Url = urlHelper;
var response = teamController.Get(1);
var contentResult = response as OkNegotiatedContentResult<DTOTeam>;
DTOTeam DtoTeam = contentResult.Content;
Team teamFromDb = new Team();
teamFromDb.Name = DtoTeam.Name;
teamFromDb.Id = DtoTeam.Id;
List<Team> teams = new List<Team>() { teamFromDb };
player = new Player() { Name = "Integration Test Player", Teams = teams };
}
示例6: GetAllPaging
public IQueryable<EventDTO> GetAllPaging(int page, int pageSize)
{
var list = _service.GetAllPaging(page, pageSize);
if (list != null)
{
var totalCount = _service.GetTotalResults();
var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
var urlHelper = new UrlHelper(Request);
var prevLink = page > 1 ? urlHelper.Link("GetAllPagingRoute", new { page = page - 1, pageSize = pageSize }) : "";
var nextLink = page < totalPages ? urlHelper.Link("GetAllPagingRoute", new { page = page + 1, pageSize = pageSize }) : "";
var firstLink = page != 1 ? urlHelper.Link("GetAllPagingRoute", new { page = 1, pageSize = pageSize }) : "";
var lastLink = page != totalPages ? urlHelper.Link("GetAllPagingRoute", new { page = totalPages, pageSize = pageSize }) : "";
var paginationHeader = new
{
TotalCount = totalCount,
TotalPages = totalPages,
PrevPageLink = prevLink,
NextPageLink = nextLink,
FirstPageLink = firstLink,
LastPageLink = lastLink
};
System.Web.HttpContext.Current.Response.Headers.Add("X-Pagination",
Newtonsoft.Json.JsonConvert.SerializeObject(paginationHeader));
}
return Queryable.AsQueryable(list);
}
示例7: GenerateLinkDirectly_ReturnsNull_IfHelperRequestHasNoConfiguration
public void GenerateLinkDirectly_ReturnsNull_IfHelperRequestHasNoConfiguration()
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/vpath/prefix/Customers");
UrlHelper urlHelper = new UrlHelper(request);
Assert.Null(urlHelper.GenerateLinkDirectly("OData", "odataPath"));
}
示例8: Get
public object Get(int page , int pageSize)
{
IQueryable<Course> query;
query = TheRepository.GetAllCourses().OrderBy(c => c.Name);
var totalCount = query.Count();
var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
var urlHelper = new UrlHelper(Request);
var prevLink = page > 0 ? urlHelper.Link("Courses", new { page = page - 1, pageSize = pageSize }) : "";
var nextLink = page < totalPages - 1 ? urlHelper.Link("Courses", new { page = page + 1, pageSize = pageSize }) : "";
var paginationHeader = new
{
TotalCount = totalCount,
TotalPages = totalPages,
PrevPageLink = prevLink,
NextPageLink = nextLink
};
System.Web.HttpContext.Current.Response.Headers.Add("X-Pagination",
Newtonsoft.Json.JsonConvert.SerializeObject(paginationHeader));
var results = query
.Skip(pageSize * page)
.Take(pageSize)
.ToList()
.Select(s => TheModelFactory.Create(s));
return results;
}
示例9: Get
//[EnableCors("*", "*", "GET")]
public HttpResponseMessage Get(int page = 0, int pageSize = 10)
{
IQueryable<Course> query;
Learning.Data.LearningContext ctx = new Learning.Data.LearningContext();
Learning.Data.ILearningRepository repo = new Learning.Data.LearningRepository(ctx);
query = repo.GetAllCourses().OrderBy(c => c.CourseSubject.Id);
var totalCount = query.Count();
var totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
var urlHelper = new UrlHelper(Request);
var prevLink = page > 0 ? urlHelper.Link("CoursesRoute", new { page = page - 1, pageSize = pageSize }) : "";
var nextLink = page < totalPages - 1 ? urlHelper.Link("CoursesRoute", new { page = page + 1, pageSize = pageSize }) : "";
var results = query
.Skip(pageSize * page)
.Take(pageSize)
.ToList();
var result = new
{
TotalCount = totalCount,
TotalPages = totalPages,
PrevPageLink = prevLink,
NextPageLink = nextLink,
Results = results
};
return Request.CreateResponse(HttpStatusCode.OK, result);
}
示例10: Get
public IHttpActionResult Get(int expenseGroupId, string fields = null, string sort = "Date", int page = 1, int pageSize = maxPageSize)
{
try
{
IQueryable<Expense> expenses = _repository.GetExpenses(expenseGroupId);
if (expenses == null)
{
return NotFound();
}
List<string> listOfFields = new List<string>();
if (fields != null)
{
listOfFields = fields.Split(',').ToList();
}
if (pageSize > maxPageSize)
{
pageSize = maxPageSize;
}
int totalCount = expenses.Count();
int totalPages = (int)Math.Ceiling((double)totalCount / pageSize);
UrlHelper urlHelper = new UrlHelper(Request);
string prevLink = page > 1 ? urlHelper.Link("ExpensesForGroup",
new
{
page = page - 1,
pageSize = pageSize,
expenseGroupId = expenseGroupId,
sort = sort
}) : "";
var nextLink = page < totalPages ? urlHelper.Link("ExpensesForGroup",
new
{
page = page + 1,
pageSize = pageSize,
expenseGroupId = expenseGroupId,
sort = sort
}) : "";
var paginationHeader = new
{
currentPage = page,
pageSize = pageSize,
totalCount = totalCount,
totalPages = totalPages,
previousPageLink = prevLink,
nextPageLink = nextLink
};
HttpContext.Current.Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationHeader));
var expensesResult = expenses
.ApplySort(sort)
.Skip(pageSize * (page - 1))
.Take(pageSize)
.ToList()
.Select(exp => _expenseFactory.CreateDataShapedObject(exp, listOfFields));
return Ok(expensesResult);
}
catch (Exception exception)
{
return InternalServerError();
}
}
示例11: Get
public HttpResponseMessage Get(int size, int page)
{
var helper = new UrlHelper(Request);
if (size > 100) size = 10;
int offset = page * size;
int next_page = page;
int prev_page = page;
//need to count max pages from total results and implement that
if (page < 1) { prev_page = 0; } else { prev_page--; }
next_page++;
var prev = helper.Link("QuestionApi", new { size = size, page = prev_page }).ToString();
var next = helper.Link("QuestionApi", new { size = size, page = next_page });
var response = Request
.CreateResponse(
HttpStatusCode.OK,
_questionRepository
.GetAllQuestions(size, offset)
.Select(question => ModelFactory.Create(question)))
;
response.Headers.Add("next-page", next);
response.Headers.Add("prev-page", prev);
return response;
}
示例12: PageLinkBuilder
public PageLinkBuilder(UrlHelper urlHelper, string routeName, object routeValues, int pageNo, int pageSize, long totalRecordCount)
{
// Determine total number of pages
var pageCount = totalRecordCount > 0
? (int) Math.Ceiling(totalRecordCount/(double) pageSize)
: 0;
// Create them page links
FirstPage = new Uri(urlHelper.Link(routeName, new HttpRouteValueDictionary(routeValues)
{
{"pageNo", 1},
{"pageSize", pageSize}
}));
LastPage = new Uri(urlHelper.Link(routeName, new HttpRouteValueDictionary(routeValues)
{
{"pageNo", pageCount},
{"pageSize", pageSize}
}));
if (pageNo > 1)
{
PreviousPage = new Uri(urlHelper.Link(routeName, new HttpRouteValueDictionary(routeValues)
{
{"pageNo", pageNo - 1},
{"pageSize", pageSize}
}));
}
if (pageNo < pageCount)
{
NextPage = new Uri(urlHelper.Link(routeName, new HttpRouteValueDictionary(routeValues)
{
{"pageNo", pageNo + 1},
{"pageSize", pageSize}
}));
}
}
示例13: Get
public object Get(int userid,int notificationid = 0, int page = 0)
{
var userName = _identityService.CurrentUser;
var baseQuery = TheRepository.GetOrganizationNotifications(userName)
.OrderByDescending( c => c.DateAdded);
var totalCount = baseQuery.Count();
var totalPages = Math.Ceiling((double) totalCount/PAGE_SIZE);
var helper = new UrlHelper(Request);
var preUrl = page > 0 ? helper.Link("Notifications", new {page = page - 1}) : "";
var newUrl = page < totalPages - 1 ? helper.Link("Notifications", new { page = page + 1 }): "";
var results = baseQuery
.Skip(PAGE_SIZE * page)
.Take(PAGE_SIZE)
.ToList()
.Select(c => TheModelFactory.Create(c));
return new
{
TotalCount = totalCount,
TotalPages = totalPages,
PrevPageUrl = preUrl,
NextPageUrl = newUrl,
Results = results,
};
}
示例14: ToResponse
public static OptionResponse ToResponse(this Option<Team> option, UrlHelper urlHelper = null)
{
var response = new OptionResponse
{
CutOff = option.CutOff,
GameId = option.GameId,
GroupId = option.GroupId,
Id = option.Id,
IsPastCutOff = option.IsPastCutOff,
IsWinner = option.IsWinner,
Outcome = option.Outcome,
Team = option.Team,
Type = option.Type,
Value = option.Value
};
if (option.Pick != null && urlHelper != null)
{
response.Pick = option.Pick.ToResponse(urlHelper, response.IsPastCutOff);
}
if (urlHelper != null && !response.IsPastCutOff)
{
response.Actions.Add("pick", urlHelper.Route("DefaultNfl", new { controller = "picks", id = option.Id }));
}
return response;
}
示例15: ODataFeedSerializerTests
public ODataFeedSerializerTests()
{
_model = SerializationTestsHelpers.SimpleCustomerOrderModel();
_customerSet = _model.FindDeclaredEntityContainer("Default.Container").FindEntitySet("Customers");
_customers = new[] {
new Customer()
{
FirstName = "Foo",
LastName = "Bar",
ID = 10,
},
new Customer()
{
FirstName = "Foo",
LastName = "Bar",
ID = 42,
}
};
_customersType = new EdmCollectionTypeReference(
new EdmCollectionType(
new EdmEntityTypeReference(
_customerSet.ElementType,
isNullable: false)),
isNullable: false);
_urlHelper = new Mock<UrlHelper>(new HttpRequestMessage()).Object;
_writeContext = new ODataSerializerWriteContext(new ODataResponseContext()) { EntitySet = _customerSet, UrlHelper = _urlHelper };
}