ASP.NET MVC custom routing with default route
I have a bit of problem
I have an MVC site that is working just fine.
I wanted to have SEO friendly urls so I created custom routing that looks like this
{section}/{group}/{manufacturer}
since it has the same number of fields as the default route
{controller}/{action}/{id}
i created in loop in RouteConfig.cs that goes to database and loops Sections table and creates custom routes like
FOOD/{group}/{manufacturer}
SPORT/{group}/{manufacturer}
CARS/{group}/{manufacturer}
and point it to the Sections controller.
It works just fine. The problem I have is that now i need to rename FOOD to FRESH-FOOD and i want to keep old links to FOOD working.
What would you recommend ? How can I solve this ?
Is there a better way to do route Section instead of custom routes ?
See also questions close to this topic
-
Incorrect syntax near 'nvarchar'. I get this error whenever trying to update a field on a database table using a grid view
This is my ASP.net code the query gets all details from the customer table and then in the grid view the user is allowed to edit the fields. Whenever i click update it throws that error then. When researching the problem i seen alot of people where saying about using spaces and [] but i tried the code with no spaces and no brackets but still get the same error. I also tried adding a @ for the parameters in the gridview Please Help.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:BookConnString %>" DeleteCommand="DELETE FROM Customer WHERE Email = @original_Email AND (([First Name] = @original_First_Name) OR ([First Name] IS NULL AND @original_First_Name IS NULL)) AND (([Surname] = @original_Surname) OR ([Surname] IS NULL AND @original_Surname IS NULL)) AND (([Address] = @original_Address) OR ([Address] IS NULL AND @original_Address IS NULL)) AND (([Postcode] = @original_Postcode) OR ([Postcode] IS NULL AND @original_Postcode IS NULL)) AND (([Gender] = @original_Gender) OR ([Gender] IS NULL AND @original_Gender IS NULL))" InsertCommand="INSERT INTO Customer ([Email], [First Name], [Surname], [Address], [Postcode], [Gender]) VALUES (@Email, @First_Name, @Surname, @Address, @Postcode, @Gender)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM Customer" UpdateCommand="UPDATE Customer SET [First Name] = @First_Name, [Surname] = @Surname, [Address] = @Address, [Postcode] = @Postcode, [Gender] = @Gender WHERE [Email] = @original_Email AND (([First Name] = @original_First_Name) OR ([First Name] IS NULL AND @original_First_Name IS NULL)) AND (([Surname] = @original_Surname) OR ([Surname] IS NULL AND @original_Surname IS NULL)) AND (([Address] = @original_Address) OR ([Address] IS NULL AND @original_Address IS NULL)) AND (([Postcode] = @original_Postcode) OR ([Postcode] IS NULL AND @original_Postcode IS NULL)) AND (([Gender] = @original_Gender) OR ([Gender] IS NULL AND @original_Gender IS NULL))"> <DeleteParameters> <asp:Parameter Name="original_Email" Type="String" /> <asp:Parameter Name="original_First_Name" Type="String" /> <asp:Parameter Name="original_Surname" Type="String" /> <asp:Parameter Name="original_Address" Type="String" /> <asp:Parameter Name="original_Postcode" Type="String" /> <asp:Parameter Name="original_Gender" Type="String" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="Email" Type="String" /> <asp:Parameter Name="First_Name" Type="String" /> <asp:Parameter Name="Surname" Type="String" /> <asp:Parameter Name="Address" Type="String" /> <asp:Parameter Name="Postcode" Type="String" /> <asp:Parameter Name="Gender" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="First_Name" Type="String" /> <asp:Parameter Name="Surname" Type="String" /> <asp:Parameter Name="Address" Type="String" /> <asp:Parameter Name="Postcode" Type="String" /> <asp:Parameter Name="Gender" Type="String" /> <asp:Parameter Name="original_Email" Type="String" /> <asp:Parameter Name="original_First_Name" Type="String" /> <asp:Parameter Name="original_Surname" Type="String" /> <asp:Parameter Name="original_Address" Type="String" /> <asp:Parameter Name="original_Postcode" Type="String" /> <asp:Parameter Name="original_Gender" Type="String" /> </UpdateParameters> </asp:SqlDataSource> </p> <p class="auto-style4"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Email" DataSourceID="SqlDataSource1"> <Columns> <asp:CommandField ShowEditButton="True" /> <asp:BoundField DataField="Email" HeaderText="Email" ReadOnly="True" SortExpression="Email" /> <asp:BoundField DataField="First Name" HeaderText="First Name" SortExpression="First Name" /> <asp:BoundField DataField="Surname" HeaderText="Surname" SortExpression="Surname" /> <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" /> <asp:BoundField DataField="Postcode" HeaderText="Postcode" SortExpression="Postcode" /> <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" /> </Columns> </asp:GridView>
-
Asp.net ListView and Bootstrap 4
I want to show a list of items on a webpage. I use a listview and each item will be show on a card. It works fine if the items are multiple of the column width otherwise I'll get one error on the last items. Please look at http://programamos.pt/cheirabem/mostraperfumes.aspx The last items will be misconfigured Here is my code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="eric.aspx.cs" Inherits="eric" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title></title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous"> </head> <body> <form id="form1" runat="server"> <div class="col-md-12 pt-1"> <div class="card"> <div class="card-header"> <h3>Perfumes enviados na embalagem original</h3> </div> <div class="card-body"> <asp:ListView ID="ListView1" runat="server" GroupItemCount="4" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1" DataSourceID="SqlDataSource1"> <EmptyDataTemplate> <table runat="server" style=""> <tr> <td></td> </tr> </table> </EmptyDataTemplate> <LayoutTemplate> <div class="row text-center"> <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder> </div> </LayoutTemplate> <GroupTemplate> <hr /> <div class="row text-center"> <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder> </div> </GroupTemplate> <ItemTemplate> <div class="col-lg-3 d-flex align-items-stretch"> <div class="card"> <div class="card-header"> <asp:Label ID="Label2" runat="server" Text='<%#Eval("sku")%>'></asp:Label> </div> <div class="card-body"> <img src="imagens/loris-azzaro-aqua-verde.jpg" width="200" height="200" /> </div> <div class="card-footer"> <asp:Label ID="Label1" runat="server" Text='<%#Eval("descricao")%>'></asp:Label> </div> </div> </div> </ItemTemplate> <AlternatingItemTemplate> <div class="col-lg-3 d-flex align-items-stretch"> <div class="card"> <div class="card-header"> <asp:Label ID="Label3" runat="server" Text='<%#Eval("sku")%>'></asp:Label> </div> <div class="card-body"> <img src="imagens/azzarohu2.jpg" width="200" height="200" /> </div> <div class="card-footer"> <asp:Label ID="Label1" runat="server" Text='<%#Eval("descricao")%>'></asp:Label> </div> </div> </div> </AlternatingItemTemplate> </asp:ListView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:perfumes %>" ProviderName="<%$ ConnectionStrings:perfumes.ProviderName %>" SelectCommand="select * from perfumes where marca="azzaro""></asp:SqlDataSource> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script> </form> </body> <asp:sqldatasource runat="server" ConnectionString="<%$ ConnectionStrings:perfumes %>" ProviderName="<%$ ConnectionStrings:perfumes.ProviderName %>" SelectCommand="select * from perfumes where marca="Azzaro""></asp:sqldatasource> </html>
-
How to cover wth unit tests StreamWriter C#
How to test the next code sample?
using (StreamReader reader = new StreamReader(requestBody, Encoding.ASCII)) { ipnContext.RequestBody = reader.ReadToEnd(); }
The very first idea comes to my mind just create custom wrapper around StreamReader and inject it to class where it's needed. What do you thinks of it. Maybe you could give me more best solution. Thanks for your attention
-
LINQ queries take more time to execute
This is my LINQ query and this is taking so long time to execute.
Can any one suggest me how to make it faster.
This query take almost 1.35 min to load.From the list I am adding in model
var jobList = option.Equals("search") ? _jobService.SearchJobs(_dataModel.SearchFilter, _dataModel.CurrentPage - 1, _dataModel.EntriesPerPage, _dataModel.KeyWords, _dataModel.Location) : _jobService.BrowsJobs(_dataModel.BrowseFilterIds, _dataModel.CurrentPage - 1, _dataModel.EntriesPerPage); _dataModel.DbJobs = jobList; _dataModel.Jobs = jobList.Select(job => new JobsViewModel { JobId = job.JobId, JobTitle = job.JobTitle, SeoFriendlyJobTitle = job.JobTitle.ToSeoFriendly(), Created = job.Created, CreatedBy = job.CreatedBy, LocationName = job.LocationId != null ? _locationService.GetById((int)job.LocationId).LocationName : string.Empty, JobTypeName = _jobTypeService.GetById(job.JobTypeId).JobTypeName, RecruiterName = job.RecruiterId != new Guid() ? _recruiterService.GetById(job.RecruiterId).RecruiterName : string.Empty, SeoFriendlyRecruiterName = job.RecruiterId != new Guid() ? _recruiterService.GetById(job.RecruiterId).RecruiterName.ToSeoFriendly() : string.Empty, RecruiterId = job.RecruiterId, StartDate = job.StartDate, LocationDescription = job.LocationDescription, SalaryDescription = job.SalaryDescription, JobSummary = job.JobSummary, JobSummaryShort = job.JobSummaryShort, LogoPath = job.Logo.IsNullOrWhiteSpace() ? job.LogoOverride : job.Logo, PremiumJob = job.Advertising.Count > 0 && job.Advertising.ElementAt(0).PremiumListing, DaysRemaining = new Helpers().BuildDaysRemainingString(job.EndDate) }).ToList();
-
Multi-Menu Not starting from the Left after the first Row in MVC
please i have an issue and any assistance would be appreciated. My sub-menu is not starting from the left on the second row. Any help would be appreciated. I have created a class that helps split the array and displays 4 columns on each row. But my problem is that after the first row, the menu doesn't start from the left but instead starts from the right.
Here's the codes below
Razor:
<div class="menu-container"> <div class="menu"> <ul> @foreach (Category cat in category) { <li> <a id="cat">@cat.Name</a> <ul> @foreach (var row in subcategories.ToArray().Split(4)) { foreach (var subcat in row) { if (subcat.CategoryId == cat.Id) { <li> <a>@subcat.Name</a> <ul> @foreach (SubCatItem item in subcatItems) { if (item.SubcategoryId == subcat.Id) { <li><a href="~/Products/AllProducts/@item.Id">@item.Name</a></li> } } </ul> </li> } } <template> <div class="clear"></div> </template> } </ul> </li> } </ul> </div> </div>
Here is my Array extension class:
public static class ArrayExtensions { /// <summary> /// Splits an array into several smaller arrays. /// </summary> /// <typeparam name="T">The type of the array.</typeparam> /// <param name="array">The array to split.</param> /// <param name="size">The size of the smaller arrays.</param> /// <returns>An array containing smaller arrays.</returns> public static IEnumerable<IEnumerable<T>> Split<T>(this T[] array, int size) { for (var i = 0; i < (float)array.Length / size; i++) { yield return array.Skip(i * size).Take(size); } } }
-
Logout multiple windows at once
I have a .Net core 2.0 MVC app and users are allowed to open the app in multiple tabs and there is a service that monitors for user idleness across the tabs.
It displays idle timer after some X mins and if the user does not take action (close the timer), he/she should be logged out.
Problem is, once the first window/tab logs the user out, they are no longer authenticated and
AuthController.Logout()
doesn’t allowAnonymousAccess
.So how will all the other windows be able to execute that method?
Adding
AllowAnonymous
to logout is the last route I want to take not knowing what implications it has. Thanks!