Url Rewriting Dynamically according value in my database - asp.net

I have successfully Developed a Dynamic web site in Asp.net.
I have Renamed the default URL as Follows.
routes.Add(new DynamicDataRoute("Dashboard.aspx")
{
Action = PageAction.List,
ViewName = "ListDetails",
//Model = DefaultModel,
Table = "SurveyKshan_Survey"
});
The problem is I want to write URL Dynamically according to the value stored in Database
Like if there is a value called NewSurvey I want my url to be like abc.com/NewSurvey and this page should throw me to the login page. Any suggestion on how to proceed.??
I am new ro asp.net any links regarding this would be appreciated.
Thanking you in advance.

Related

Set query parameter based on current URL

I am working on a standard paging component for my project. All controllers with paging are waiting for PageIndex parameter in URL.
So I want to generate URL based on the current URL except for the PageIndex parameter.
For example, I have filters for my internet store like Manufacturer and MaxPrice.
A user opens the mystore.com/products?manufacturer=Apple&MaxPrice=999 link.
Then he wants to go to the 3 pages. So the 3-page link in my paging should have the mystore.com/products?manufacturer=Apple&MaxPrice=999&PageIndex=3 link.
So needed MVC function should:
Persists all existing params like MaxPrice and manufacturer
Replace only PageIndex param
don't use any hardcoded controller and action values (like controller = 'Products', Action = 'Index')
I try to use this code:
<a class="page-link" href="#Url.RouteUrl(new { PageIndex = page })">
#page
</a>
It works fine except the 2 rule - it doesn't persist other arguments.
So if user click on this link he goes to mystore.com/products?PageIndex=3
I suggest to build the url dynamically by getting currentUrl with query strings "Request.Url.AbsoluteUri" then remove the pageIndex from url if exists , then add page index again.
hint : url must be defined as variable in your razor to make the things easier
To remove query string you can use regex
string queryString = "Default.aspx?Agent=10&Language=2"; //Request.QueryString.ToString();
string parameterToRemove="Language"; //parameter which we want to remove
string regex=string.Format("(&{0}=[^&\s]+|{0}=[^&\s]+&?)",parameterToRemove);
string finalQS = Regex.Replace(queryString, regex, "");
You can use these to get the current url
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TEST/Default.aspx
string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TEST/Default.aspx
And then you can add the page index like this and redirect to that url
url = url+"&PageIndex=3";
Recommended
Or you can get the url parameters with
#Url.RequestContext.RouteData.Values["manufacturer"]
#Url.RequestContext.RouteData.Values["MaxPrice"]
And use those values to build the new URL inside the View

Create a url that passes values to controller action

I want to create a Url that I can email that goes to an action within a controller, passing to the action an Id and a Token. This is my code so far:
var action = new UrlActionContext
{
Action = "Verify",
Controller = "Auth",
Values = id, token
};
var url = UrlHelperExtensions.Action(action);
UrlHelperExtensions.Action is expecting an IUrlHelper however, and I have not been able to get this to work using that interface. Can someone please explain how I am able to form a Url that goes to this action?
I can only seem to find solutions to Asp.Net MVC projects, whereas I am using Asp.Net Core 2.0.
For ASP.Net Core 2.0 IUrlHelper is available as a property of the controller. ControllerBase.Url is an IUrlHelper instance. You should be using it like this:
var url = Url.Action(nameof(DoSomething), new { id = 10 });

how to get the current user domain in serverside while using form authentication?

I have an Application where i need to find the current Log in Users Domain name in the server site, but my authentication mode is Form, kindly help me out.
Finally i thought a nice way of solving the problem, and got it done too.
By using JS we can get the current system details then store it into a hidden filed and we can access the current system domain name & user name in the server side.
function domain_Data() {
var comdetails = new ActiveXObject("wscript.shell");
var comusername = comdetails.ExpandEnvironmentStrings("%username%");
var comusername1 = comdetails.ExpandEnvironmentStrings("%UserDomain%");
var add = comusername1 + ":" + comusername;
alert(add);
document.getElementById("_hide").value = add;}

ASP.NET Dynamic Data site within Umbraco 6

I have a Dynamic Data site in a folder called admin. This folder is in the root of the website and referenced in the reserved paths section of the web.config file.
After upgrading from Umbraco 4.7.2 to 6.0.5 I've noticed that the links in the Dynamic Data site that normally take me to my tables are now trying to hit the /umbraco/rendermvc/List controller and action. I'm assuming that somehow my routes have been changed, but being so new to MVC I have no idea how to restore these.
If it is any help, this is the section of my startup code that used to register the contexts correctly. Any help on how to restore these routes without breaking the routing of the new Umbraco version would be very appreciated!
public static void RegisterContext(RouteCollection routes, string dbName, Type contextType, string ddFolder)
{
var model = new MetaModel
{
DynamicDataFolderVirtualPath = ddFolder,
FieldTemplateFactory =
new FieldTemplateFactory()
{TemplateFolderVirtualPath = "~/admin/DynamicData/FieldTemplates",}
};
model.RegisterContext(contextType, new ContextConfiguration() {ScaffoldAllTables = true});
routes.Add(new DynamicDataRoute("admin/{dbname}/{table}/{action}.aspx")
{
Constraints = new RouteValueDictionary(new
{
action = "List|Details|Edit|Insert",
dbname = dbName
}),
Model = model
});
Models[dbName] = model;
}
I think you have to put your custom stuff in an override on the OnApplicationStarted event in a custom global.asax which inherits from Umbraco.Web.UmbracoApplication (I haven't tried it yet), see this blog (about half way down the page) and our.umbraco.

Dynamic sitemap, database driven

I've been struggling with this for a couple of days now. Can't find any good example, or an example that I understand.
Background:
I own a small blog platform for user to blog.
Each user gets their own subdomain and for now there is no sitemap available. Not good.
I want to create some kind of dynamic sitemap, where all sitemapnodes is retreived from the database. The sitemap will be used only for the search engine spiders.
System: ASP.NET, mySQL.
The sitemap is pure XML. So I need in some way to create an ASPX file that return xml-data instead of html.
And I need to somehow redirect the web.sitemap to that dynamic file.
I have never worked with XML, and I dont know how to create a file that creates XML data. So i dont even know what to search for.
I don't want any static sitemap file to be stored on the server. Everything should be created on the fly.
So. Please. If you can give me some advise about XML, any example on the internet, or just what to search for.
My main questions:
1.
How to create XML output from aspx file?
2.
How do I "inform" the system, and search engine crawlers that the file to crawl is "/sitemap.aspx"
ThankS!
I looked into MvcSiteMapProvider.MVC5 and I could not get it to work. First of all it modified my Web.config to the point that my css and js files were getting a 404 not found when running my web app.
With the time I spent getting MvcSiteMapProvider to work I could have just wrote my own.
So... here is my own dumbed down version of generating a sitemap xml.
The only thing is you have to specify your routes manually. I haven't added reflection yet to go through each controller and pull out each action.
The data-driven piece works very well though.
In your Home controller add the action Sitemap and the private helper methods.
GetRouteUrls is the manually added controller/action routes.
GetDynamicUrls builds the data-driven Urls. In my example I have a LiquidsController and a Details(string id) action.
public ActionResult Sitemap()
{
var xml = new XDocument(
new XDeclaration("1.0", "utf-8", null),
new XElement("urlset",
new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
, GetRouteUrls()
, GetDynamicUrls()
)
);
return new XmlActionResult(xml);
}
private List<XElement> GetDynamicUrls()
{
var result = new List<XElement>();
using (var db = new ApplicationDbContext())
{
var liquids = db.Liquids.ToList();
foreach (var liquid in liquids)
{
result.Add(LocUrl("Liquids", "Details", liquid.FriendlyId));
}
}
return result;
}
private List<XElement> GetRouteUrls()
{
var result = new List<XElement>();
result.Add(LocUrl("Account", "Register"));
result.Add(LocUrl("Account", "Login"));
result.Add(LocUrl("Home", "Index"));
result.Add(LocUrl("Home", "About"));
result.Add(LocUrl("Home", "Contact"));
result.Add(LocUrl("Home", "TermsOfService"));
result.Add(LocUrl("Home", "PrivacyStatement"));
result.Add(LocUrl("Liquids", "Index"));
result.Add(LocUrl("Vendors", "Index"));
result.Add(LocUrl("Hardware", "Index"));
return result;
}
private XElement LocUrl(string controller, string action, string id = null)
{
if (!string.IsNullOrEmpty(id))
action = string.Format("{0}/{1}", action, id);
var baseUri = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));
return new XElement("url",
new XElement("loc", string.Format("{0}{1}/{2}", baseUri, controller, action))
);
}
I then added a route so I could access the sitemap doing /sitemap
routes.MapRoute(name: "sitemap", url: "sitemap", defaults: new {controller = "Home", action = "Sitemap"});
The XmlActionResult return type can be found here:
Return XML from a controller's action in as an ActionResult?

Resources