How to save pages with unique names? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i am using C# and sql database. In db is stored information about items - id, name, etc.
Now i have a page to show items, that i use as a template and pass Item id in the url like /Item.aspx?id=223.
I would like to create pages for every item and save them to folder like /Items/Red-Book.aspx.
Any suggestions?
Thanks, Walt

You don't need to save the pages physically. You only need one page Item.aspx which will generate the content according to the ItemiId you supply. (the same way you explained in the question)
After you have this page you need to look into how to rewrite URL.
What rewrite URL does is that it takes the url that a user requested and rewrites it to something else.
If a user request /Items/Red-Book.aspx the url will be rewrited to /Item.aspx?id=223 and youre ASP.NET will load /Item.aspx?id=223 back to the client.
You can make use of the method RewritePath which you can call at Application_BeginRequest to rewrite your URL.The logic would be as follow:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
var currentPath = HttpContext.Current.Request.Path;
// do some logic to generate newPath
var newPath = GetNewPath ( currentPath );
HttpContext.Current.RewritePath(newpath);
// after this point ASP.NET will work as the user would have requested newpath
}

Related

How to access database in asp.net mvc? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
In the default asp.net application, I want to access the data of other users. In my database table ASPNETUSERS I have columns such as Id, Email, Password, Gender etc. Now I want to be able to search for a particular Id and get that user's entire details like his/her Email, Password, Gender etc. So how do I do that?
Do I use raw SQL? If so how? (someone on SO mentioned this as a bad idea)
Do I use models or something else? if so how?
This is my ASPNETUSERS Table info. And this is my action in a controller. I want to be able to get an user called "stackoverflow#gmail.com" and send all his details(whatever are there in my table) to the view.
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult Index(string id = "")
{
//db.someCommand or something to let me get stuff about
//stackoverflow#gmail.com. Could it be db.Database.ExecuteSqlCommand ?
return View("Send All details about my stackoverflow user");
}
Edit: This is what I tried before. I tried to use raw sql and convert it to a string(for testing) and write it to a file on desktop. That way I thought I would at least know that the data in my database is getting passed back to me correctly before I pass it to my view.
This was my line of code that I added just before my action returned.
System.IO.File.WriteAllText(#"C:\Users\Admin\Desktop\log.txt", db.Database.ExecuteSqlCommand("select * from dbo.AspNetUsers").ToString());
I did not get any errors doing so, but I expected my data from the image above to be posted into my file called log.txt on desktop. But I opened log.txt file only to find that "-1" was posted in there.
If you want to get the users from your database, use linq with your ApplicationDBContext object
ApplicationDbContext db = new ApplicationDbContext();
//Manipulate the Where clause to get the conditions you want
IEnumerable<ApplicationUser> users = db.Users.Where(x => x.Email == "stack#overflow.com");
This works with the out of the box ASP.NET individual authentication model (which looks like what you have)

How to Commit changes of HTML code permanently ? C# ASP.NET [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
First I create a page with a textbox and a button; when the button is clicked, it redirects to another page.
This is the code for the redirected page:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
TextBox SourceTextBox =
(TextBox)Page.PreviousPage.FindControl("TextBox");
if (SourceTextBox != null)
{
form1.InnerHtml = SourceTextBox.Text;
}
}
}
Now, I've what is written in the text box is displayed but the changes in the form aren't permanent. When I close the page then I open it again, it doesn't display what I have written before.
Is there any way to make the changes in the form permanent when I use .innerHtml?
Not without adding some storage solution. All you're doing here is editing the inner HTML of the form in the single instance of the page being rendered on the server, any other users loading the page will not see your changes, and as you have already discovered reloading the page yourself discards the content and reloads from the server.
You could save the content to a file on the server, store it in a database on your server, or perhaps use an online solution like Google's Firebase if you don't have access your your own DB server.

How to make website readonly that link from certain websites. asp.net [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a website where a user logs in, can see his information and edit it. There is also another website, something like a site only for admins, where if I click a link it redirects me to the first website, logged in as that user but I can only read his information not edit it. I am having trouble finding out how to make website 1 both readable only and read/writeable.
I am doing this in Asp.NET mvc using C#.
One method would be to check for authorization in the Razor view.
Psuedocode:
#if(User.IsAuthorizedForEdit()
{
#*your edit view code*#
}
else
{
#*your readonly view code*#
}
This does make for some bloaty Razor. The other (arguably, better) alternative is to direct them to the appropriate view in your controller based on user.
Handy-wavy psuedocode to give you an idea:
public ActionResult ViewProfile(int profileId)
{
var user = GetCurrentUser();//without looking at your code, I can't infer this piece.
var profile = GetProfile(profileId);
if(IsAuthorizedToEdit(user, profileId)
{
return View("edit", profile);
}
else
{
return View("view", profile);
}
}
In theory, you already have a read-only view and an edit view, so the latter would be more reusable.

First page with QueryString [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to load index page with QueryString in asp.net? I know that we can redirect to a particular page with QueryString, but what I want is to load first page with some querystring.
If you are setting start action in Property pages of your application then you can follow following steps
1) right click on your project in solution explores
2) Go to Property pages
3) Set start action to 'Specific Page' and value = "index.aspx?a=22"
A very simple way to make it work on both local and remote enviroments is to, at page_load(), detect if the desired QueryString content is present.
If not, Use Response.Redirect pointing to the current page with the added QueryString parameters. Example follows:
if (Request.QueryString["QSEntry"] == null)
Response.Redirect("Page.aspx?QSEntry=desiredValue");
Pro: It'll work the way you want.
Con: You're actually loading the page twice (first time it's a parameterless load), so don't forget to take that into consideration.

Execute controller method without raise Session_Start event [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have some logic in Session_Start and this logic is actual for all my controller methods except one special method. I need to not execute Session_start logic, when user goes to special method URL.
Any ideas how I can do that?
As far as I understand your question, you do not want the code within your Session_Start method to be invoked if a special url is requested. I think it would be helpful to know what your problem is, that you want to solve. For now here is my answer:
Since Session_Start is only invoked once (at least usually, depending on your configuration of the session module - see my comments to your question), this only works if the client invokes the "special" url first, e.g. before calling other urls. If another url has been invoked first, session will be initialized according to your code. Important: as mentioned, depending on your configuration, there will be always a Session (but in this special case you do not want to execute your custom logic in Session_Start):
You can use the Current HttpRequest and perform a check on some properties:
// this will (usually) only be called once, on the first request of the client
protected void Session_Start() {
// perform your check here if this is the url you want to exclude
if (HttpContext.Current.Request.Url.OriginalString.ToLowerInvariant().EndsWith("something")) {
return;
}
// your initialization here that should not be executed for clients accessing the site using the above url
}
As you can see, you can access the Request object, and perform your check there, depending on your requriements.

Categories